#!/usr/bin/perl
#
# Small Makefile to cat the needed sub_routines from ./src/
#
# Tue Jan 15 20:13:39 CET 2002 Martin J. Muench (mjm@remote-exploit.org)

# Oh yeah, be hard, be tough, just be #
use strict;

# var for OS Detection #
my $IsBSD = 0;
## Location of my configdir and create it##
my $confdir=%ENV->{HOME}."/.wellenreiter";
## Location of the configfile ##
my $conffile=$confdir ."/.wellenreiter.rc";

my $dev2typ; #-- needed in different parts of the script
## Function prototyping
sub conf;
sub common;

##-- Begin of Main --##

## Welcome screen
print "\n************************************************************";
print "\n*                                                          *";
print "\n* Wellenreiter config generator by team remote-exploit.org *";
print "\n*                                                          *";
print "\n*             http://www.remote-exploit.org                *";
print "\n*                                                          *";
print "\n************************************************************";
print "\n\n              Press the return to continue "; sleep until <STDIN>;

##-- Check that the user is root, because saving of config needs to be in roots home --##
if (($> != 0 || $< != 0)) 
{
	print "\n!!! -> Sorry, you have to run this as root....exiting... <- !!!\n\n";
	exit;
}

## Delete old config files not in the directory if they exist ##
my $oldfilename= %ENV->{HOME}."/.wellenreiter.rc";
if (open (OLDFILE,$oldfilename))
{
	unlink ($oldfilename);
}

## If the directory no exists create it  ##
unless (opendir(DHANDLE,$confdir))
{
	mkdir $confdir,700 || die "Could not create $confdir :".$! . "\n";
}

## Create configfile
open(CONF, ">$conffile") || die "Error while opening config-file: $!";
print CONF "#############################################################\n";
print CONF "#                                                           #\n";
print CONF "#          This is the config file of Wellenreiter          #\n";
print CONF "#  It has been automaticly created using perl config.pl     #\n";
print CONF "# Please do not modify this file, if you dont know what you #\n";
print CONF "# are doing! If you simply want to change the cardtype etc, #\n";
print CONF "# re-run perl config.pl from the Wellenreiter source dir.   #\n";
print CONF "#                                                           #\n";
print CONF "#############################################################\n";
print CONF "# Team remote-exploit.org:                                  #\n";
print CONF "#                          http://www.remote-exploit.org    #\n";
print CONF "#############################################################\n\n";

if ($^O =~ m/bsd/) { 
	print "\nBuilding Wellenreiter for BSD...\n\n";
	$IsBSD = 1;
	conf;
}
elsif ($^O =~ m/linux/) {
	print "\nBuilding Wellenreiter for Linux...\n\n";
	conf;
}
else { print "Unknown OS, try to build your config-file manually\n"; }

## Everythings done
close(CONF); exit(1);

##-- End of Main --##

sub conf 
{

  # Get the card type for iface1
  print "\nChoose your wireless card type [cisco|prism2|lucent] [default: cisco] : ";
  chomp(my $devtyp=<STDIN>);
  my $buttonstobuild;
  if(!$devtyp || $devtyp eq 'cisco') 
  {
  	print CONF "iface1typ=cisco\n";
	  if ($IsBSD == 1)
	  {
		  print "\nSet your wireless interface name [default: an0] : ";
		  chomp(my $device=<STDIN>);
		  if(!$device) { print CONF "interface=an0\n"; }
	 	  else { print CONF "interface=$device\n"; }
		  $buttonstobuild='Noiselevel,Signallevel,Linkquality,MISC-Stats,Card-Config,Scanner,About';
	  }
	  else
	  {
	      print "\nSet your wireless interface name [default: eth1] : ";
	  	chomp(my $device=<STDIN>);
	  	if(!$device) { print CONF "interface=eth1\n"; }
	  	else { print CONF "interface=$device\n"; }
	 	$buttonstobuild='Linkquality,Signallevel,RX-Stats,TX-Stats,MISC-Stats,Card-Config,Scanner,Batterymon,About';
	  }
  }
  elsif ($devtyp eq 'prism2')
  {
  	print CONF "iface1typ=prism2\n";
	  if ($IsBSD == 1)
	  {
		  print "\nSet your wireless interface name [default: wi0] : ";
		  chomp(my $device=<STDIN>);
		  if(!$device) { print CONF "interface=wi0\n"; }
	 	  else { print CONF "interface=$device\n"; }
		  $buttonstobuild='Noiselevel,Signallevel,Linkquality,MISC-Stats,Card-Config,Scanner,About';
	  }
	  else
	  {
	  	print "\nSet your wireless interface name [default: wlan0] : ";
	  	chomp(my $device=<STDIN>);
	  	if(!$device) { print CONF "interface=wlan0\n"; }
	  	else { print CONF "interface=$device\n"; }
  		$buttonstobuild='RX-Stats,TX-Stats,MISC-Stats,Card-Config,Scanner,Batterymon,About';
	  }
  }
    elsif ($devtyp eq 'lucent')
  {
  	print CONF "iface1typ=lucent\n";
	  if ($IsBSD == 1)
	  {
		  print "\nSet your wireless interface name [default: wi0] : ";
		  chomp(my $device=<STDIN>);
		  if(!$device) { print CONF "interface=wi0\n"; }
 	 	  else { print CONF "interface=$device\n"; }
		  $buttonstobuild='Noiselevel,Signallevel,Linkquality,MISC-Stats,Card-Config,Scanner,About';
	  }
	  else
	  {
		  print "\nSet your wireless interface name [default: eth1] : ";
		  chomp(my $device=<STDIN>);
		  if(!$device) { print CONF "interface=eth1\n"; }
		  else { print CONF "interface=$device\n"; }
	  	  $buttonstobuild='Linkquality,Signallevel,Noiselevel,RX-Stats,TX-Stats,MISC-Stats,Card-Config,Scanner,Batterymon,About';
	  }
  }
  else
  {
  	print "\nSorry you entered something strange...please enter either \"cisco\" or \"prism2\" or \"lucent\"";
  	print "\nThe program cannot continue.....aborting...";
  	exit;
  }

  # Got compatible sniffing card ?
  print "\nDo u got a RAW-capture rfmon compatible card? \nIf you say no to this question you \ncannot use scan/sniff functions of wellenreiter (read README) [y/N] : ";
  chomp(my $got_raw_card=<STDIN>);
  if(!$got_raw_card || $got_raw_card eq 'n')
  { 
  	print CONF "got_raw_card=n\n"; 
  	$buttonstobuild =~ s/Scanner,//;
  }
  else 
  { 
  	print CONF "got_raw_card=y\n"; 

	  # Ask for the cannels to switch in between #
	  print "\n Witch is the highest available channel in your country for wireless networks?\n"
	       . "in Europe normaly 13, in USA normaly 11 [default: 13]: ";
	  chomp (my $maxchannel=<STDIN>);
	  if (!$maxchannel)
	  {
		print CONF "maxchannel=13\n";
	  }
	  else
	  {
		print CONF "maxchannel=$maxchannel\n";
	  }
	  
  	# Get the card type for iface2
	   print "\nChoose your type for this card [cisco|prism2|lucent] [default: cisco] : ";
	   chomp(my $dev2typ=<STDIN>);
   	if(!$dev2typ || $dev2typ eq 'cisco') 
  		{
			  if ($IsBSD == 1)
	 		 {
		  		print "\nSet your wireless interface name [default: an0] : ";
		  		chomp(my $device=<STDIN>);
		  		if(!$device) { print CONF "sniffer_device=an0\n"; }
	 	 		else { print CONF "sniffer_device=$device\n"; }
		  		$buttonstobuild='Noiselevel,Signallevel,Linkquality,MISC-Stats,Card-Config,Scanner,About';
	  		}
	  		else
	  		{
				  print "\nSet your wireless interface name [default: eth2] : ";
		 		 chomp(my $sniffer_device=<STDIN>);
	  			if(!$sniffer_device) 
	  			{ 
	  				print CONF "sniffer_device=eth2\n";
	  			}
	  			else 
	  			{   
	  				print CONF "sniffer_device=$sniffer_device\n";
	  			}

				#-- some newer cisco drivers got the wifi0 device for sniffing --#
				  print "\nDoes your cisco driver support the wifi0 interface?\n".
				        "This device is available on newer versions. Check it by\n".
				        "inserting the card and type \"ifconfig -a\". If you can\n" . 
				        "see a line with \"wifi0\", then you should answer Y to this\n".
				        "otherwise you should choose N [y/N] : ";
				        chomp(my $wifidevice=<STDIN>);
				        if(!$wifidevice)
				        {
				        	print CONF "wifidevice=n\n";
				        }
				        else
				        {
				        	print CONF "wifidevice=$wifidevice\n";
				        }

			   }
	  		 #-- Write type to conf --#
  			 print CONF "iface2typ=cisco\n";
 	 		 #-- Write the default settings for bring cisco cards in sniffing mode --#

			   if ($IsBSD == 1)
		 	  {
   	 	  	  # Command to bring $sniffer_device into rfmon mode
  				  print CONF 'start_sniff_command=' . 'ancontrol -i %sniffer_device% -M 5 && ifconfig an0 1.1.1.1' ."\n";
			        # Command to stop $sniffer_device from rfmon mode
   	 			print CONF 'stop_sniff_command=' .'ancontrol -i %sniffer_device% -M 0' . "\n";
			 	   # Command to swich the channel 
	    			print CONF 'channel_switch_command=' .'ancontrol -i %sniffer_device% -c %channel%' . "\n";
			    }
				else #-- If its linux --#
				{		  
	       	  	  # Command to bring $sniffer_device into rfmon mode
  				  print CONF 'start_sniff_command=' . 'echo "Mode: r" > /proc/driver/aironet/%sniffer_device%/Config && echo "Mode: y" > /proc/driver/aironet/%sniffer_device%/Config && echo "XmitPower: 1" > /proc/driver/aironet/%sniffer_device%/Config' . "\n";
			   	  # Command to stop $sniffer_device from rfmon mode
   	 			  print CONF 'stop_sniff_command=' .'echo "Mode: ESS" > /proc/driver/aironet/%sniffer_device%/Config' . "\n";
			 	  # Command to swich the channel 
	    			  print CONF 'channel_switch_command=' .'echo "Channel: %channel%" > /proc/driver/aironet/%sniffer_device%/Config' . "\n";
			    }
			
  		}
  		elsif ($dev2typ eq 'prism2')
  		{
  			print CONF "iface2typ=prism2\n";
			  if ($IsBSD == 1)
	 		 {
				  print "\nSet your wireless interface name [default: wi0] : ";
				  chomp(my $sniffer_device=<STDIN>);
				  if(!$sniffer_device) 
				  { 
					print CONF "sniffer_device=wi0\n"; 
				  }
			 	  else 
				  { 
					print CONF "sniffer_device=$sniffer_device\n"; 
				  }
		  		  $buttonstobuild='Noiselevel,Signallevel,Linkquality,MISC-Stats,Card-Config,Scanner,About';
	  		 }
	  		 else
	  		 {
				  print "\nSet your wireless interface name [default: wlan1] : ";
		 		 chomp(my $sniffer_device=<STDIN>);
	  			if(!$sniffer_device) 
	  			{ 
	  				print CONF "sniffer_device=wlan1\n";
	  			}
	  			else 
	  			{   
		  			print CONF "sniffer_device=$sniffer_device\n";
		  		}
			  }
			  if ($IsBSD == 1)
	 		  {
			  	#-- Write the default settings for bring prism cards in sniffing mode --#
        	  		# Command to bring $sniffer_device into rfmon mode
  				print CONF 'start_sniff_command=' . '/usr/local/bin/prism2ctl %sniffer_device% -m'."\n";
		   	   	# Command to stop $sniffer_device from rfmon mode
   	 			print CONF 'stop_sniff_command=' .'/usr/local/bin/prism2ctl %sniffer_device% -h'."\n";
		 	     	# Command to swich the channel 
	    			print CONF 'channel_switch_command=' .'/usr/sbin/wicontrol -i %sniffer_device% -f %channel%'."\n";
			  }
			  else
			  {
			  	#-- Write the default settings for bring prism cards in sniffing mode --#
        	  		# Command to bring $sniffer_device into rfmon mode
  				print CONF 'start_sniff_command=' . '/sbin/wlanctl-ng %sniffer_device% lnxreq_wlansniff channel=6 prismheader=false keepwepflags=false enable=true > /dev/null'."\n";
		   	   	# Command to stop $sniffer_device from rfmon mode
   	 			print CONF 'stop_sniff_command=' .'/sbin/wlanctl-ng %sniffer_device% lnxreq_wlansniff channel=6 enable=false > /dev/null'."\n";
		 	     	# Command to swich the channel 
	    			print CONF 'channel_switch_command=' .'/sbin/wlanctl-ng %sniffer_device% lnxreq_wlansniff channel=%channel% prismheader=false keepwepflags=false enable=true > /dev/null' . "\n";
			  }
 		 }
  		elsif ($dev2typ eq 'lucent')
  		{
  			print CONF "iface2typ=lucent\n";
			  if ($IsBSD == 1)
	 		  {
			    print "Sorry, no scanning support for Lucent yet\n";
			    exit;
	  		  }
			  else
			  {
			  	print "\nSet your wireless interface name [default: eth2] : ";
	 		 	chomp(my $sniffer_device=<STDIN>);
  				if(!$sniffer_device) 
  				{ 
  					print CONF "sniffer_device=eth2\n";
  				}
  				else 
  				{   
  					print CONF "sniffer_device=$sniffer_device\n";
	  			}
			 }			

			  if ($IsBSD == 1)
		 	  {
			    # Not yet supported, Lucent stuff will be placed here
		 	  }
			  else
			  {
		  	  #-- Write the default settings for bring prism cards in sniffing mode --#
 	       	 	  # Command to bring $sniffer_device into rfmon mode
  			  print CONF 'start_sniff_command=' . 'iwpriv %sniffer_device% monitor 2 11'."\n";
		 	  # Command to stop $sniffer_device from rfmon mode
   		 	  print CONF 'stop_sniff_command=' .'iwpriv %sniffer_device% monitor 0 11'."\n";
		 	  # Command to swich the channel 
	    		  print CONF 'channel_switch_command=' .'iwpriv %sniffer_device% monitor 2 %channel%' . "\n";
		 	  }
		  }
  		else
  		{
  			print "\nSorry you entered something strange...please enter either \"cisco\" or \"prism2\"";
  			print "\nThe program cannot continue.....aborting...";
  			exit;
  		}


	  }

	#-- End of interface config, now start with path config --#
	  print CONF "mwbuttons=$buttonstobuild\n";


	  # Get the path to ifconfig
      	  print "\nTry to get the path to your ifconfig ......";
	  my $path_ifconfig = `which ifconfig`; 
	  $path_ifconfig =~ s/\n//g; 
	  if ($path_ifconfig)
	  {
	  	print CONF "ifpath=$path_ifconfig\n";
	  }
	  else
	  {
	  	print "\n Cannot find the path to ifconfig, set path to ifconfig [default: /sbin/ifconfig]: ";
	  	chomp($path_ifconfig=<STDIN>);
	  	if(!$path_ifconfig) { print CONF "ifpath=/sbin/ifconfig\n"; }
	  	else { print CONF "ifpath=$path_ifconfig\n"; }
	  }

	  # Get the path to iwconfig
	  if ($IsBSD == 1)
 	  {
		  if ($dev2typ eq 'cisco')
		  {
		  	print "\nTry to get the path to ancontrol now ...";	
		  	my $path = `which ancontrol`; 
		  	$path =~ s/\n//g; 
		  	if ($path)
		  	{
			 	 print CONF "iwpath=$path\n";	  		
		  	}
		  	else
			  {
			  	print "\n Cannot find the path to ancontrol, set path to ancontrol [default: /usr/sbin/ancontrol]: ";
				  chomp($path=<STDIN>);
	  			if(!$path) { print CONF "iwpath=/usr/sbin/ancontrol\n"; }
	  			else { print CONF "iwpath=$path\n"; }
		  	}
		  }
		  else
		  { 
			  print "\nTry to get the path to wicontrol now ...";	
			  my $path = `which wicontrol`; 
			  $path =~ s/\n//g; 
			  if ($path)
			  {
				  print CONF "iwpath=$path\n";	  		
			  }
			  else
			  {
		 	 	print "\n Cannot find the path to wicontrol, set path to wicontrol [default: /usr/sbin/wicontrol]: ";
				  chomp($path=<STDIN>);
	  			if(!$path) { print CONF "iwpath=/usr/sbin/wicontrol\n"; }
	  			else { print CONF "iwpath=$path\n"; }
		 	 }
		 }
	 }
	 else 
	 {
		 print "\nTry to get the path to iwconfig now ...";
		  my $path = `which iwconfig`; 
		  $path =~ s/\n//g; 
		  if ($path)
		  {
			  print CONF "iwpath=$path\n";	  		
		  }
		  else
		  {
		  	print "\n Cannot find the path to iwconfig, set path to iwconfig [default: /sbin/iwconfig]: ";
		  	chomp($path=<STDIN>);
  			if(!$path) { print CONF "iwpath=/sbin/iwconfig\n"; }
  			else { print CONF "iwpath=$path\n"; }
	  	}
	 }

  #-- Default Gps entries --#
  print CONF "got_gps=n\n";
  print CONF "gpsdport=2947\n";

  # Do u like to enable the audio features?
  print "\n\nActivate accoustic features? [Y/n] : ";
  chomp(my $audio=<STDIN>);
  if(!$audio || $audio eq 'y') 
  {
  	print CONF "enable_audio=1\n";
		print "\nCommand to execute when a broadcasting network is found? \ndefault [printf \"\\\\a\"] : ";
		chomp (my $broadfound =<STDIN>);
		if (!$broadfound || $broadfound eq 'printf "\\a"' || $broadfound eq 'printf "\\a"')
		{
			print CONF "c_new_broadcast_netfound=printf \"\\a\"\n";
		}
		else
		{
			print CONF "c_new_broadcast_netfound=$broadfound\n";
		}

		print "\nCommand to execute when a NON-broadcasting network is found? \ndefault [printf \"\\\\a\"] : ";
		chomp (my $nbroadfound =<STDIN>);
		if (!$nbroadfound || $nbroadfound eq 'printf "\\a"' || $nbroadfound eq 'printf "\\a"')
		{
			print CONF "c_new_nonbroadcast_netfound=printf \"\\a\"\n";
		}
		else
		{
			print CONF "c_new_nonbroadcast_netfound=$nbroadfound\n";
		}

		print "\nCommand to execute when a new object is found? \ndefault [printf \"\\\\a\"] : ";
		chomp (my $nbjfound =<STDIN>);
		if (!$nbjfound || $nbjfound eq 'printf "\a"' || $nbjfound eq 'printf "\\a"')
		{
			print CONF "c_objectfound =printf \"\\a\"\n";
		}
		else
		{
			print CONF "c_objectfound=$nbjfound\n";
		}

		print "\nCommand to execute when a non-broadcasting essid is uncoverd? \ndefault [printf \"\\\\a\"] : ";
		chomp (my $uncoverd =<STDIN>);
		if (!$uncoverd || $uncoverd eq 'printf "\\a"' || $uncoverd eq 'printf "\\a"')
		{
			print CONF "c_uncoverd_essid=printf \"\\a\"\n";
		}
		else
		{
			print CONF "c_uncoverd_essid=$uncoverd\n";
		}

		print "\nCommand to execute when a beacon drops in default [printf \"\\\\a\"] : ";
		chomp (my $bindicator =<STDIN>);
		if (!$bindicator || $$bindicator eq 'printf "\a"' || $bindicator eq 'printf "\\a"')
			{
				print CONF "c_beacon_indicator=printf \"\\a\"\n";
			}
			else
			{
				print CONF "c_beacon_indicator=$bindicator\n";
			}
			print CONF "accoustic_beacon_indicator=no\n";
  }
  else
  {
  	print CONF "enable_audio=0\n"
  }

# Enough for now, the rest is done by hand
  print "All necessary things are now configured\n\n";
  print "\n\tYou can now run perl Wellenreiter.pl\n\n\n";

print CONF qq~

#############################################################
### You can change the following parameters if you want ####
### to change the default layout of the windows, but    ###
### it should match for anyone. If you want to be safe  ##
### leave it as it is.                                  #
########################################################

#-- Nightcolors rgb values --#
nightcolor_r=58 
nightcolor_g=49 
nightcolor_b=49 

#-- Mainwindow layout --#
#How many buttons to display in a single row on the mainwindow?
columncount=1

#-- Reiceive-Statistics-Window layout --#
rxcolumn=4
rxtitle=Receive-Statistics

#-- Send-Statistics-Window layout --#
txcolumn=4
txtitle=Send-Statistics

#-- MISC-Statistics-Window layout --#
misccolumn=4
misctitle=Misc-Statistics

#-- About-Window layout --#
abouttitle=About

#-- Config-Window layout --#
configtitle=Configuration
configcolumn=4

#-- Signallevel-Window layout --#
signaltitle=Signallevel

#-- Linkquality-Window layout --#
linktitle=Linkquality

#-- Noiselevel-Window layout --#
noisetitle=Noiselevel

#-- This is the interval in milliseconds when the autosave should take place --#
autosaveinterval=6000

##########################################################
###       Don't change anything below this line       ###
########################################################

#-- General definitions --#
progname=Wellenreiter
version=1.6

#-- Linux-wireless-extension-path  -  This should be right for all--#
wirelessfile=/proc/net/wireless

#-- Netpackets statistics file  -  This should be right for all --#
netstatsfile=/proc/net/dev

##########################################################
###             End of the config file                ###
########################################################
~;
}
