#! /usr/bin/perl #


#

start dit script op met:

curl -s http://wLeiden.NET/genesis/ | perl --

#


#! /usr/bin/perl
#
# aug 2002 jasper@WirelessLeiden.NL

$version='0.3';

#$DEBUG=1;


$start=localtime();
$home='/etc/wl';
$url='http://wLeiden.NET/genesis';
#$rpmurl='ftp://dl.xs4all.nl/pub/mirror/redhat/redhat-7.3-en/os/i386/RedHat/RPMS';
$rpmurl='ftp://dl.xs4all.nl/pub/mirror/redhat/linux/7.3/en/os/i386/RedHat/RPMS/';

@Packages=(
'wget-1.8.1','curl-7.9.5','openssh-server-3.1p1','openssh-clients-3.1p1',
'joe-2.9.7','zebra-0.92a-3','mrtg-2.9.17',
'tcpdump-3.6.2','squid-2.4.STABLE6-1.7.2',
'bind-9.2.0','apache-1.3.23','rdate-1.2-1',
'dhcp-2.0pl5'
);

@MRTGJUNK=('cfgmaker.html','contrib.html','faq.html','forum.html','index.html',
'indexmaker.html','logfile.html','mibhelp.html','mrtg-rrd.html','mrtg.html',
'mrtglib.html','nt-guide.html','reference.html','squid.html','unix-guide.html',
'webserver.html');

@MRTGIMG=('mrtg-l.gif','mrtg-l.png','mrtg-m.gif','mrtg-m.png','mrtg-r.gif',
'mrtg-r.png','mrtg-ti.gif','mrtg-ti.png');


$curl='/usr/bin/curl';
$inittab='/etc/inittab';
$sshd_config='/etc/ssh/sshd_config';
$authorized_keys='/root/.ssh/authorized_keys';
$syslogd_conf='/etc/syslog.conf';
$rclocal='/etc/rc.d/rc.local';
$sc_network='/etc/sysconfig/network';
$crontab='/etc/crontab';
$liloconf='/etc/lilo.conf.anaconda';
$mrtgdir='/var/www/html/mrtg';
$passwd='/etc/passwd';
$shadow='/etc/shadow';

$var_named='/var/named';
$var_named_slave="$var_named/slave";

$hostname=`/bin/hostname`;chomp($hostname);$hostname=~s/\..*//;

#select(STDOUT); $| = 1; 

printc( "\n\n===== Wireless Leiden Genesis $version for $hostname =====\n\n");
printc("In the beginning there was /etc/wl");

my_mkdir ($home,0750);

######################         DIVERSEN    #########################


if ($ARGV[0]) 
{
  $hostname=$ARGV[0];
  changeline($sc_network,"HOSTNAME","HOSTNAME=$hostname");
  system("/bin/hostname $hostname.wleiden.net");
  prints ("New hostname: $hostname");
}


$node=`$curl -s $url/nodes/$hostname/`;
#print "NODE: [$node]\n";

if (($node=~/found on this server/) || ($hostname eq ''))
{
   print "\n\nHostname \"$hostname\" unknown on genesis system.\n";
   print "Please restart with hostname as agument:\n\n";
   print "curl -s http://wLeiden.NET/genesis/ | perl -- - [hostname] \n\n";
   die;
}

#goto skip;

#####################    PACKAGES  ####################################

foreach $package (@Packages) {InstallIfNotThere($package);}

InstallIfNotThere("bing-1.0.4","ftp://rpmfind.net/linux/contrib/libc6/i386");
InstallIfNotThere("fping-2.4b2","ftp://rpmfind.net/linux/PLD/current/dists/ra/PLD/i386/PLD/RPMS");
#InstallIfNotThere("ntop-2.1-1","http://belnet.dl.sourceforge.net/sourceforge/ntop/");
InstallIfNotThere("wavemon-0.3.3-1","ftp://rpmfind.net/linux/contrib/libc6/i386/");
#InstallIfNotThere("idled-1.16-3","ftp://rpmfind.net/linux/PLD/current/dists/ra/PLD/i386/PLD/RPMS/");

AddIfNotThere($inittab,'S0:123:respawn:/sbin/agetty -L 9600 ttyS0');
AddIfNotThere($sshd_config,'PermitRootLogin yes');
AddIfNotThere($syslogd_conf,'*.*                             /dev/tty9');
AddIfNotThere($rclocal,'echo 60 > /proc/sys/kernel/panic');

$doit='/usr/bin/rdate -s ntp.xs4all.nl;/sbin/clock --systohc --utc';
system($doit);
printc ("Checking /etc/crontab");
if (! filegrep('rdate',$crontab))
{
   AddIfNotThere($crontab,"13 6 * * *      root $doit");
}

printc ("Checking $liloconf"); if (! filegrep('timeout=15',$liloconf)) {
 changeline($liloconf,'timeout','timeout=15'); system("/sbin/lilo -C
 $liloconf");
}

# Jasper:
#

AddIfNotThere($authorized_keys,'1024 35
112316178287877254070956100425865829142003519260838089408287428288025179160927766087063935502299240797274832024419201965494823824334871043373834738280496449985305215259303428888378450891325700353005777564637335420689043600277656759878355825032152723795185677246322116557728089284732770412581611008730473076369
jasper@Foxtrot',0600); AddIfNotThere($authorized_keys,'1024 35
118239913251009869865766536315021393707936886074250129280418473820786192419898669632268750479860782257598739847197103203348900588986714533679311193658890557424225585046131153077993767644932126481282093252667491668462126364793663058473958394234134729276158925678092948983706464884277485578826546212336317771647
jasper@Rumba'); AddIfNotThere($authorized_keys,'1024 35
131115877365044889919364159918102221501007851129140588698864391358409700706033130394488227521530533168939153736569754398766671594746877715705568124401657833947746079021505059669681909865480458530856596287241882611975228914550983484838927508168402688375217383947586789572836291240495264780490011259940976676783
jasper@Tango');

# Rudi:
#

AddIfNotThere($authorized_keys,'1024 35
175064508659828994627143321524163097781640785788870666512791607844470773231705101704418403768320192803174740066957164497121494748184227124076884950318600937518771055598445142736335521690474047053323012366268793076855215471573920191862419906922134514121969462360529797078932635823683742743592103014922572913173
rudi@flupke'); AddIfNotThere($authorized_keys,'1024 35
143166375720170866349619197277385566768069875485196362699778322322352272754425517150081365776757054647703778864422036993461721791436126915970412582719415351906955315394823309461075762059061244401928288023541130196242097703041933109276811888686865551081981845265381340343445264057143590813968740962614380418301
rudi@quick'); AddIfNotThere($authorized_keys,'1024 35
124752035168971877893059147471720282336429451383717387223954769248711511091174717193477105333864864131448165452070009395515515659396169159755357937833999410182494957674293616658655374538654124880362749453138940491080015307589195673993882335454433811025091710719877693183773703706788265932234293023384253606179
rudi@Classic');


AddIfNotThere($passwd,'croot:x:0:0:root:/root:/bin/bash',0,'croot',1);
AddIfNotThere($passwd,'rudi:x:1500:100::/home/rudi:/bin/bash',0,'rudi',1);
AddIfNotThere($passwd,'jasper:x:1501:100::/home/jasper:/bin/bash',0,'jasper',1);

AddIfNotThere($shadow,'croot:$1$0ngjFP1K$NFBRKYS5Ltdd6ohgf67i31:11918:0:99999:7:::',0,'croot',1);
AddIfNotThere($shadow,'rudi:$1$Ni3ÁëñÍß$OnUTJgV3k.394Z1qDY.Vz0:11910:0:99999:7:::',0,'rudi',1);
AddIfNotThere($shadow,'jasper:$1$hZdZY2yU$5MsprMarQcBE42NicTxib.:11881:0:99999:7:::',0,'jasper',1);


##########################   MRTG #############################3

my_mkdir ($mrtgdir,0750); chdir($mrtgdir); unlink(@MRTGJUNK); foreach $file
(@MRTGIMG) {
  if (! -e "$mrtgdir/$file") {copyfile2("mrtg/$file",$mrtgdir,644);}
}
copyfile2("mrtg/cpuload.pl","/usr/local/sbin",755);
copyfile2("mrtg/uptime.pl","/usr/local/sbin",755);
copyfile2("mrtg/ipchains_traf.pl","/usr/local/sbin",755);
copyfile2("mrtg/latency.pl","/usr/local/sbin",755);
copyfile2("mrtg/memusage.pl","/usr/local/sbin",755);
copyfile2("mrtg/throughput.pl","/usr/local/sbin",755);
copyfile2("mrtg/uptime.pl","/usr/local/sbin",755);


copyfile2("mrtg/wlan_bitrate.pl","/usr/local/sbin",755);
copyfile2("mrtg/wlan_quality.pl","/usr/local/sbin",755);
copyfile2("mrtg/wlan_snr.pl","/usr/local/sbin",755);
copyfile2("mrtg/wlan_users.pl","/usr/local/sbin",755);

AddIfNotThere($crontab,"*/5 * * * *      root /usr/bin/mrtg /etc/mrtg/mrtg.cfg",0,'mrtg');



######################         NAMED    #########################

my_mkdir ($var_named,0750);
my_mkdir ($var_named_slave,0750);
($login,$pass,$uid,$gid) = getpwnam('named'); 
chown($uid,$gid,$var_named_slave) || 
		die "Could not change perms $var_named_slave to $uid/$gid";

copyfile2('named/named.conf','/etc/',0644);
copyfile2('named/localhost.zone','/var/named/',0644);
copyfile2('named/127.0.0.zone','/var/named/',0644);
copyfile2('named/root.hint','/var/named/',0644);
copyfile2('named/resolv.conf','/root/',0644);

if (! -e '/etc/rc3.d/S55named')
{symlink('../init.d/named','/etc/rc3.d/S55named') || 
			die "Could not symlink named.";}

system("/etc/init.d/named restart"); # Direct zones gaan laden. --> cache

######################         APACHE    #########################

copyfile2('apache/httpd.conf','/etc/httpd/conf/');

######################         SQUID    #########################

copyfile2('squid/squid.conf','/etc/squid/');

######################         NETWERK    #########################


copyfile2("nodes/$hostname/ifcfg-eth0","/etc/sysconfig/network-scripts/");

copyfile2("tools/netmask","/usr/local/bin",0750);

skip: copyfile2("areas.conf","/etc/wl/",0644);
copyfile2("nodes/$hostname/wleiden.conf","/etc/wl/",0644);
copyfile2("tools/wleiden.pl","/etc/wl/",0750);
copyfile2("tools/firewall","/etc/wl/",0750);
copyfile2("tools/nofirewall","/etc/wl/",0750);

copyfile2("tools/wl-runlvl","/etc/init.d/wireless",0750);
if (! -e '/etc/rc3.d/S30wireless')
{symlink('../init.d/wireless','/etc/rc3.d/S30wireless') || 
			die "Could not symlink wireless.";}




printc("Starting /etc/wl/wleiden.pl\n");
system("/etc/wl/wleiden.pl startall");



######################             #########################


# TODO:
#
# httpd
# mrtg
# eth0, wlan*
# zebra
# firewall

exit;

##########################################################################

sub printc
{
   prints(@_);
   $counter++;
}

sub prints
{
  local($line,$nlf)=@_;
  local($time)=localtime();  
  ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);

  printf ("[%02d %02d:%02d:%02d] $line",$counter,$hour,$min,$sec);
  if ($nlf eq '') {print"\n";}
}

sub filegrep
{
 local($grep,$file)=@_;
 local($found)=0;
 open(FILE,$file) || die"Could not grep in file: $file\n"; 
 while()
 {
   if (/$grep/) {$found=1;last;}
 }
 close(FILE);
 return($found);
}

sub add2file
{
   local($file,$line,$nocr)=@_;
   open (FILE,">>$file") || die "Coult not open $file\n";
   if (! $nocr) {print FILE "\n";}
   print FILE "$line\n";
   close(FILE);

}

sub AddIfNotThere
{
  local($file,$line,$mode,$search,$nocr)=@_;
  if (! $search) {$search="^$line";}
  if (($mode) && (! -e $file)) # Create file?
  {
     print("Creating file with $line\n");  
     add2file($file,$line);
     chmod($mode,$file);
  } else
  {
  printc("Checking $file: ",1);
  if (! filegrep("$search",$file))
  {  
   print("Adding $line\n");  
   add2file($file,$line,$nocr);
  } else {print "OK!\n";}
  }
}


sub InstallIfNotThere
{
  local($package,$ru)=@_;if (! $ru) {$ru=$rpmurl;}
  local($rpm);
  printc("Checking for rpm $package: ",1);
  $rpm=`rpm -q $package`;
  if ($rpm=~/not installed/) 
  {
	print "installing now.\n";
	system("rpm -i $ru/${package}*-i386.rpm");
  } else {print "OK: $rpm";}
}

sub copyfile2
{
  local($src,$dst,$perm)=@_;if (! $perm) {$perm=0640;}
  local($fn,$ztime);
  if (-d $dst)
  {
    $dst=~s/\/$//;
    if ($src=~/\/([^\/]+)$/) {$dst.="/$1";} else {$dst.="/$src";}
  }

  printc("Copying $src to $dst");

  if (-e $dst) {$ztime="-z $dst";} 

  if ($DEBUG) {print("DEBUG: $curl $ztime $url/$src -o $dst\n");}
  system("$curl -s -f $ztime $url/$src -o $dst");
  if (! -f $dst) {die "Error copying file $src to $dst.\n";}
  chmod($perm,$dst) || die "Could not change permission for $dst to $perm.\n";
}

sub my_mkdir
{
   local($dir,$perm)=@_;if (! $perm) {$perm=0755;}
   prints ("Checking dir: $dir: ",1);
   if (! -d $dir) {mkdir ($dir,$perm) || die "Could not make dir: $dir\n";print "created.\n";}
   else {print "OK!\n";}
}

sub changeline
{
   local($file,$old,$new)=@_;
   open(OLD,$file) || die "Could not open old file: $file\n";
   open(NEW,">$file.genesis.$$") || die "Could not create new file: $file.genesis.$$\n";
   while()
   {
	if (/$old/) {print NEW "$new\n";}
	else {print NEW $_;}
   }
   close(NEW);
   close(OLD);
   rename("$file.genesis.$$",$file) || die "Coult not overwrite old file: $file\n";
}





#