#!/usr/bin/perl -w
#
# Author: George Sipos email: xvudpapa --AT--savba --DOT-- sk
# Version: 1.0
# 
# What's this? This is a Perl script to configure simple ISDN connection
# from a local FreeBSD machine to your ISP. It is also possible that
# this script may be used on any BSD machine.
#
# This program is free; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version. This software is distributed in 
# the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
# PURPOSE. See the GNU General Public License for more details.

# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

print "\We're going to check if you are root:\n";
if (!$<) {
print "You are root. Now we can start with ISDN configuration!\n";
}
else {
print "You're not root. Go to see your administrator. Good Bye!\n";
exit;
}

####
## Kernel information ####
####

 print "-----------------------------------------------------------------";
 print "\nCOPYRIGHT (c) GEORGE SIPOS, November 2  2001,  xvudpapc\@savba.sk";
 print "\nThis is a  script  to make  a quick dialup connection to your ISP";
 print "\nthrough ISDN on a BSD machine;  the script was tested on FreeBSD.";
 print "\nAll more complicated  stuff as  telephony,  monitor, rates,  etc.";
 print "\nhave to be configured  on  the  basis of information you  find in";  
 print "\nthe ISDN4BSD manual. Now we are going to make an exemplary kernel"; 
 print "\nfor a PNP PCI card based on a Winbond W6692 chi[.If you use other";
 print "\ncard, consult the  documentation. Basically, you write  something"; 
 print "\nlike 'device isic0'  to your  kernel  file  for  Elsa   PCI card.";
 print "\nW6692 uses the 'iwic' driver. As I do not know if you have kernel"; 
 print "\nsources installed on your system  in /usr/src/sys  (FreeBSD),  or"; 
 print "\nwhich BSD system you use,  we  are going to create a file in your"; 
 print "\nroot directory, MYKERNEL, so you just copy the entries from  this"; 
 print "\nfile to your FreeBSD /usr/src/sys/i386/conf directory  where  you";
 print "\nwill have your own kernel file (e.g., MYKERNEL); please learn how";
 print "\nto compile kernel. After you copy the ISDN entries to your kernel";
 print "\nfile, run the command  'config  /usr/src/sys/i386/conf/MYKERNEL',";
 print "\nthen you should cd to the directory /usr/src/sys/compile and  you"; 
 print "\nshould  run   the   command    'make  depend',  'make'  and  then"; 
 print "\n'make install'. Now  answer  the  question,   do   you   have  an"; 
 print "\nISDN4BSD entry in your kernel file? You must be root  to do this!";
 print "\nAnswer 'y' (yes) (any other key to continue) or 'n' (no): ";
 
 $entry = <STDIN>;
 chomp($entry);
 $entry =~ tr/A-Z/a-z/;
 if ($entry eq "n") {
 print "\f\n\nYour new ISDN kernel entry will be put to the /root/MYKERNEL file.\n";
 print "NOW RECOMPILE YOUR KERNEL AND START THIS SCRIPT AGAIN.\n\a";
 open(FILE, ">/root/MYKERNEL");
 print FILE "device iwic0", "\n";
 print FILE "\n# The 'iwic0' driver is only an example, 'iwic0' will";
 print FILE "\n# change to 'iwic 1' in FreeBSD 5.0 and above,";
 print FILE "\n# similarly, 'isic0' will change to 'isic 1'.";
 print FILE "\n# i4b passive ISDN cards support contains the following drivers:\n";
 print FILE "\n# isic  - Siemens/Infineon ISDN ISAC/HSCX/IPAC chipset driver";
 print FILE "\n# iwic  - Winbond W6692 PCI bus ISDN S/T interface controller";
 print FILE "\n# ifpi  - AVM Fritz!Card PCI driver";
 print FILE "\n# ihfc  - Cologne Chip HFC ISA/ISA-PnP chipset driver";
 print FILE "\n# ifpnp - AVM Fritz!Card PnP driver";
 print FILE "\n# itjc  - Siemens ISAC / TJNet Tiger300/320 chipset\n";
 print FILE "\npseudo-device  ", '"i4bisppp"', "  4", "\n";
 print FILE "pseudo-device  ", '"i4bq921"',  "\n";
 print FILE "pseudo-device  ", '"i4bq931"',  "\n";
 print FILE "pseudo-device  ", '"i4b"',      "\n";
 print FILE "pseudo-device  ", '"i4btrc"',   "  4", "\n";
 print FILE "pseudo-device  ", '"i4bctl"',   "\n";
 print FILE "pseudo-device  ", '"i4brbch"',  "  4", "\n";
 print FILE "## userland driver for telephony, now we're not going to use this\n";
 print FILE "#pseudo-device ",  '"i4btel"',   " 2", "\n";
 print FILE "## enable VJ header compression detection for ipr i/f\n";
 print FILE "options         IPR_VJ\n";
 print FILE "## enable logging of first 32 ip packets for ipr i/f's\n";
 print FILE "options        IPR_LOG=32\n";
 print FILE "pseudo-device  ", '"i4bipr"',    " 2", "\n";
 close(FILE);
 exit(0);
 } 
#### 
## ISP and ISDN information ###
####
 print "\nThe user name your Internet Service Provider gave you: ";
 $user_name = <STDIN>;
 chomp($user_name);
  
 print "\nPassword your ISP gave you, be careful, Unix is case sensitive: ";
 $password = <STDIN>;
 chomp($password);

 print "\nISP name, do not omit, this is important for reference. ";
 print "\nJust anything can be written here: ";
 $isp = <STDIN>;
 chomp($isp);
 $isp =~ tr/a-z/A-Z/;
 
 print "\nISP gave you 'pap' (password auth. protocol) or 'chap' protocol?";
 print "\nBoth protocols pertain to authentication with ISP."; 
 print "\nIf unsure, use 'pap': ";
 $ap = <STDIN>;
 $ap =~ tr/A-Z/a-z/;
 chomp($ap);
 while ($ap ne "pap" && $ap ne "chap") {
 print "\nYou must write 'pap' or 'chap', no other alternative is possible.\n";
 print "\nWrite 'pap' or 'chap': ";
 $ap = <STDIN>;
 chomp($ap);
   }
		 
 print "\nYour MSN, any of few ISDN numbers your telecom company gave you,";
 print "\nif you make a mistake here, your connection will not work,";
 print "\none telephone number is sufficient to put here: ";
 $msn = <STDIN>;
 chomp($msn);

 print "\nISDN channel (1, 2, or 0 for any channel): ";
 print "\n(If unsure, say '0'): ";
 $channel = <STDIN>;
 chomp($channel);
#we're checking the numeric input
 while ($channel !~ m/\d/ || $channel > 2 || $channel < -1) {
 print "\nEnter the correct values, 1 or 2, 0 for any: ";
 $channel = <STDIN>;
 chomp($channel);
  }
		  
 print "\nISP Phone Number you use for dialing your ISP: ";
 $phone = <STDIN>;
 chomp($phone);
 
 print "\nISP destination address.  The  destination  address will be used ";
 print "\nby the 'ifconfig' command  of the 'ipppup' script we will create ";
 print "\nin /usr/local/bin directory. If you do not know your destination ";
 print "\nIP address, put here a bogus destination  address,  for  example,"; 
 print "\n64.58.76.227 and put the 'debug'  option  in  the 'ipppup' script";
 print "\nwe will create, so  the  ifconfig  command  in  the ipppup script";
 print "\nwill look something like:"; 
 print "\nifconfig isp0 0.0.0.0 64.58.76.227 netmask 0xffffff00 link1 debug";
 print "\nAfter running the script, when the connection is  made,  you will";
 print "\nhave the real destination address in your /var/log/messages  file";
 print "\nimmediately somewhere near the occurrence of words 'do not agree'";
 print "\nand you should run this script again and put the real destination";
 print "\naddress, or you can just edit  the 'ipppup' script  and  put  the"; 
 print "\naddress there. Write here your ISP's destination address: ";
 $desta = <STDIN>;
 chomp($desta);
 
 print "\nDialing reaction - what happens if someone dials in? ";
 print "\nWrite: accept, reject, ignore, answer: ";
 $dreact = <STDIN>;
 chomp($dreact);
 $dreact =~ tr/A-Z/a-z/;
 while ($dreact ne "accept" && $dreact ne "reject" && $dreact ne "ignore" && $dreact ne "answer") {
   print "\nYou must type this correctly: accept, reject, ignore or answer? ";
   $dreact = <STDIN>;
   chomp($dreact);
  }

 print "\nDo you want outgoing, incoming or both directions?";
 print "\nIf you say 'in' direction only, a dialout may not work!";
 print "\nWrite 'in', 'out', or 'inout': ";
 $direct = <STDIN>;
 chomp($direct);
 $direct =~ tr/A-Z/a-z/;
   while ($direct ne "inout" && $direct ne "out" && $direct ne "in") {
   print "\nYou must type this correctly, the 'in' option may not work ";
   print "\nwith a dialout. Say 'out', 'inout' or 'in' if you know what you're doing: ";
   $direct = <STDIN>;
   chomp($direct);
 print "\nGood, now we continue!\n";
   }
	     
 print "\nDNS Number your ISP gave you (0.0.0.0): ";
 $number = <STDIN>;
 chomp($number);

 print "\nPut here your second DNS number. Leave blank if inappropriate: ";
 $number2 = <STDIN>;
 chomp($number2);
 
 print "\nWhat is the Domain of your DNS (www.domain.com)? ";
 $dns = <STDIN>;
 chomp($dns);

####
##Let's make sure this all is correct
####

 print "Please check that the following information is correct: \n\n\a";
 print "User Name: $user_name\n";
 print "User Password: $password\n"; 
 print "ISP Name: $isp\n";
 print "ISP gave you the following protocol: $ap\n";
 print "Your MSN: $msn\n";
 print "ISDN channel you want to use: $channel\n";
 print "ISP's Phone Number: $phone\n";
 print "Your ISP's destination address: $desta\n";
 print "Dialing reaction: $dreact\n";
 print "Dialing direction: $direct\n";
 print "Domain Name: $dns\n";
 print "DNS Number: $number\n";
 print "DNS Number2: $number2\n";

 print "Is this information Correct? (y/n) ";
 $q = <STDIN>;
 chomp($q);
 $q =~ tr/A-Z/a-z/;

 if ($q eq "n") {
   print "Run the script again\n\a";
   exit;
 }
 if ($q eq "y") {
  print "Generating /etc/isdn/isdnd.rc file\n";
  }
 else {
  print "Run the script again\n";
  exit;
  }
####
## Let's delete old files made by this script if running it again.
## Let's check you have /etc/isdn directory on your system, if not
## we're going to create it.
####
 
 if (-d "/etc/isdn") {
 print "\nYou have /etc/isdn directory, O.K.\n";
     }
 else { system("mkdir /etc/isdn");
     }
 if (-f "/etc/isdn/isdnd.rc") {
 system("mv /etc/isdn/isdnd.rc /etc/isdn/isdnd.rc.old");
     }
 if (-f "/usr/local/bin/ipppup") {
 system("rm /usr/local/bin/ipppup");
     }
 if (-f "/etc/resolv.conf") {
 system("mv /etc/resolv.conf /etc/resolv.conf.old");
     }
 if (-f "/usr/local/bin/ipppdown") {
 system("rm /usr/local/bin/ipppdown");
     }
 
 print "Please Wait while isdnd.rc file is created . . .\n\n";
 print "Creating /etc/resolv.conf . . .\n";
 open(FILE, ">>/etc/resolv.conf");
 print FILE "\domain ",$dns,"\n";
 print FILE "nameserver ",$number, "\n";
 
 if ($number2 ne "") {
 print FILE "nameserver ",$number2,"\n";
   }
    close(FILE);
####
## We are creating the /etc/isdn/isdnd.rc file
####
  print "Creating /etc/isdn/isdnd.rc . . .\n";
  open(FILE, ">>/etc/isdn/isdnd.rc");
  print FILE "system", "\n";
  print FILE "\nuseacctfile = yes", "\n";   # info	
  print FILE "acctall = on", "\n";	  # generate info for everything
  print FILE "acctfile = /var/log/isdnd.acct", "\n";   # name & location of accounting file
  print FILE "rtprio = 25", "\n";         # modify isdn priority
  print FILE "monitor-allowed = yes", "\n";   # global switch: monitor on/off
  print FILE "monitor-port = 451", "\n";  # default monitor TCP port
  print FILE "monitor = ", '"/var/run/isdn-monitor"', "\n";
  print FILE "monitor-access  = fullcmd", "\n";
  print FILE "monitor-access  = channelstate, logevents", "\n";
  print FILE "monitor-access  = callin, callout", "\n";
  print FILE "monitor = ", '"10.0.0.1/24"', "\n";
  print FILE "monitor-access  = restrictedcmd, channelstate, callin, callout", "\n";
  print FILE "\nentry", "\n";
  print FILE "\nname = $isp", "\n";
  print FILE "usrdevicename = isp", "\n";           # ipr, isp, tel, rbch
  print FILE "usrdeviceunit = 0", "\n";             # unit number
  print FILE "isdncontroller = 0", "\n";            # contoller to use or -1 to use any
  print FILE "isdnchannel   = $channel","\n";       # channel (1/2) to use or 0 or -1 for an
  print FILE "direction = $direct", "\n";           # in, out, inout
  print FILE "local-phone-incoming = $msn", "\n";
  print FILE "remote-phone-incoming = * ", "\n";    # this one can call in
  print FILE "local-phone-dialout = $msn", "\n";      # this is my number
  print FILE "remote-phone-dialout = $phone", "\n";   # i call this remote number
  print FILE "dialin-reaction=ignore", "\n";        # accept, reject, ignore, answer, callba
  print FILE "dialout-type = normal", "\n";           # normal / calledback
  print FILE "b1protocol = hdlc", "\n";               # hdlc, raw
  print FILE "dialretries = 0", "\n";                 # of dial retries
  print FILE "dialrandincr = off", "\n";              # random dial increment time
  close(FILE);

####
## We are creating the script that makes an Internet connection
####
  print "Creating /usr/local/bin/ipppup . . .\n\n";
  open(FILE, ">>/usr/local/bin/ipppup");
  print FILE "#!/bin/sh\n";
  print FILE "\n# ppp isdn setup script\n";
  print FILE "isdnd; sleep 2\n";
  print FILE "\necho ", '"configuring I4BISDN/PPP-interface isp0:"', "\n";
  print FILE "\necho ", '"--------------------------------------"', "\n";
  print FILE "\necho ", '""', "\n";
  print FILE "echo ", '"removing previous configuration"', "\n";
  print FILE "ifconfig isp0 delete -link1 down", "\n"; 
  print FILE "\necho ", '""', "\n";
  print FILE "echo ", '"setting PPP options"', "\n";
  print FILE "ispppcontrol isp0 myauthproto=$ap", "\n";
  print FILE "ispppcontrol isp0 myauthname=$user_name", "\n";
  print FILE "ispppcontrol isp0 myauthsecret=$password", "\n";
  print FILE "ispppcontrol isp0 hisauthproto=none", "\n";
  print FILE "\necho ", '""', "\n";
  print FILE "echo ", '"configuring IP src/dst address, netmask and link flags"', "\n";
  print FILE "ifconfig isp0 0.0.0.0 $desta netmask 0xffffff00 link1" , "\n";
  print FILE "route add default -interface isp0", "\n";
  print FILE "\n#The ping command is useful to see that the network is working\n";
  print FILE "ping 147.213.1.22\n";
  print FILE "echo ", '""', "\n";
# print FILE "echo ", '" finished"'; "\n";

####
## We are creating the script that shuts down the Internet connection
####
  print "Creating /usr/local/bin/ipppdown . . .\n\n";
  open(FILE, ">>/usr/local/bin/ipppdown");
  print FILE "#!/bin/sh\n";
  print FILE "\n# Script for shutting down our ISDN dialup connection\n";
  print FILE "\nkillall isdnd";
  close(FILE);

####
## We are making these scripts executable
####
  $command_line = "chmod +x /usr/local/bin/ipppup";
  system($command_line);
  
  $command_line2 = "chmod +x /usr/local/bin/ipppdown";
  system($command_line2);
    
  print "\nNOW OPEN YOUR ISDN DIALUP CONNECTION TO YOUR ISP BY RUNNING\n";
  print "\nTHE SCRIPT /usr/local/bin/ipppup", "\n"; 
  print "\nSHUT DOWN YOUR ISDN CONNECTION BY RUNNING THE SCRIPT\n";
  print "\n/usr/local/bin/ipppdown, BOTH SCRIPTS ARE IN YOUR PATH!\n";
  print "\nREMOVE THE 'PING' COMMAND FROM YOUR 'ipppup' SCRIPT IF THE CONNECTION\n";
  print "\nIS WORKING WELL!\n";
  