#!/usr/bin/perl #*************************************************************************** # Virtfs - A Perl/Dialog Script # ------------------- # Started : December 09 2000 # This Version : v. 0.30.0 # copyright : (C) 1999 by Afra Ahmad # email : afra@prongs.org # homepage : http://prongs.org/virtfs # *************************************************************************** #*************************************************************************** # # This program is free software; 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. # # *************************************************************************** use strict; my $version="0.30.2"; #version number # The configuration file - change this if need be. my $config_file="/etc/virt.conf"; # The paths of the executable applications below. # Try changing the names ( in the ("") ) if virtadmin complains. # Setting $delete_root to 1 makes sure the root account is not accidentaly # deleted. To override this feature, set the $delete_root to 0, but be # careful! my $delete_root = 1; ### END CONFIGURATION - any more changes need to be done to $config_file above! my $default_shell=&lookfor("Default Shell"); my $default_home=&lookfor("Default Home Directory"); my $mail_folder=&lookfor("Default Mail Folder"); my $mail_grp=&lookfor("Default Mail Group"); my $virtual_dir=&lookfor("Leading Virtual Directory",1); my $tmp=&lookfor("Log",1); my $backtitle="--backtitle \"Virtual Services - virtfs v.$version \""; my $server_file=&lookfor("Servers File",1); $server_file="$virtual_dir/$server_file"; if ($ARGV[0] eq "detect"){ &detect_new(); exit(0); } elsif (($ARGV[0] ne "detect") && ($ARGV[0] ne "")){ print "virtfs $version\n"; print "Please use:\n\nvirtfs - Start configuring virtual domains.\n"; print "virtfs detect - detect all virtual domains.\n\n"; exit(0); } my $ftp_file=&lookfor("FTP file",1); $ftp_file="$virtual_dir/$ftp_file"; my $pop_file=&lookfor("MAIL file"); $pop_file="$virtual_dir/$pop_file"; if (!-d $virtual_dir){ my $text = "The virtual directory $virtual_dir \ndoes not exist!\n\n"; $text .= "Should I create this for you?\n"; my ($mk_dir)=&yes_no($backtitle,$text, "10 50"); if ($mk_dir == 0){ mkdir "$virtual_dir",0755 || die "Couldn't create directory!"; open (FH, "+>> $server_file") || die "Couldn't edit $server_file: $!\n"; print FH "# Created by virtfs - please don't edit this unless you know what you're doing\n"; close (FH); } else { exit(0);} } my ($domainname,$ip)=&list_domains($virtual_dir); my ($domain)="$virtual_dir/$domainname"; my $chroot=&find_path("chroot"); my $adduser=&find_path("adduser"); my $userdel=&find_path("userdel"); my $su=&find_path("su"); my $passwd=&find_path("passwd"); my $sendmail=&find_path("sendmail"); &menu_list; # start the menu sub menu_list{ #show the menu, get the choice, and do what the user asks for. # cancel exits, btw... my ($choice)=&list_menu($domain); &look_choice($choice); } sub detect_new{ my @dirs=<$virtual_dir/*>; my $i; for ($i=0;$i<@dirs;$i++){ if (-d "$dirs[$i]/etc"){ print "$dirs[$i]"; &compare_server($dirs[$i]); } } } sub compare_server{ my ($domain)=@_; my ($d,$ip,$found); open (SD, "+< $server_file"); while (){ chomp; s/#.*//; s/^s+//; s/\s+$//; ($d,$ip)=split(/\:/,$_); if ($domain =~ /$d/){ print " ... detected and exists in configuration..."; close (SD); $found = 1; } } if ($found != 1){ close (SD); $domain =~ s#.*/##; print " .. detected and does not exist\n"; print "Please enter the IP number for $domain: "; my $ip=;chomp $ip; open (FH,"+>>$server_file"); print FH "$domain:$ip\n"; close (FH); print "Added.\n"; } print "\n"; } sub conf_files{ # Find out if a domain is configured for a certain service # and display the results to the user. my ($conf)=@_; open (FD, "+< $conf"); my ($i,$s); while (){ if ($_ =~ /$domain/){ return "Yes"; } } return "No"; } sub look_choice{ # We take the user input from the menu and execute accordingly... my ($choice)=@_; my ($text); if ($choice eq "I"){ # simply display the server information... $text .= "Server Name: $domainname\n"; $text .= "Server IP: $ip\n"; $text .= "Server Directory: $domain\n"; my ($ftp)=&conf_files($ftp_file); $text .= "FTP Active: $ftp\n"; my ($mail)=&conf_files($pop_file); $text .= "Mail Active: $mail\n"; &info ($backtitle,$text,"15 65"); } elsif ($choice eq "G"){ #finger a list of accounts. ($text)=&list_accounts($domain); my $stat=&finger_account($text); } elsif ($choice eq "T"){ ($text)=&list_accounts($domain); my $stat=&mail_folder($text); } elsif ($choice eq "R"){ # delete the server. Ask the user twice for confirmation, # and if (s)he agrees, remove the entry and delete the # domain's directory. Use with caution!! $text .= "Remove Server?"; my $status=&yes_no ($backtitle,$text); if ($status == 0){ $text = "Are you sure?\n(last chance)"; $status=&yes_no($backtitle,$text); if ($status==0){ &remove_server_entry(); system("rm -rf $domain"); &delete_conf_entry($ftp_file); &delete_conf_entry($pop_file); print "Server Deleted.....\n"; exit(0); } } } elsif ($choice eq "C"){ # alter all configuration files to reflect the new domain name and IP address... my $text = "Please enter the domain name (server.com):\n"; my $newdomain=&input($backtitle,$text,"$domainname","12 55"); #&list_menu if ($newdomain == 0); $text = "Current IP: $ip\n"; my ($stat,$newip); do{ $newip=&input($backtitle,$text,"","12 55"); $stat=&check_ip_format($newip); }until ($stat == 1); &remove_server_entry(); &delete_conf_entry($ftp_file); &delete_conf_entry($pop_file); open (FH, "+>> $server_file"); print FH "$newdomain:$newip\n"; close (FH); system ("echo $newdomain > $domain/etc/HOSTNAME") if ($newdomain ne $domainname); system ("mv $domain $virtual_dir/$newdomain") if ($newdomain ne $domainname); print "\nServer name changed $newdomain.\nIP changed to $newip."; print "\nDirectory has been changed to reflect this.\n Re-configure IP aliasing and then reset FTP/Mail services through virtfs\n"; exit(0); } elsif ($choice eq "U"){ # simply list all users. ($text)=&list_users($domain); &text_box("Users",$text,"20 60"); } elsif ($choice eq "S"){ # su over to the domain as a specific user (you will be asked). &su_exec($domain); } elsif ($choice eq "A"){ # Add a user! &add_user($domain); } elsif ($choice eq "P"){ ($text)=&list_accounts($domain); &change_passwd($text,$domain); } elsif ($choice eq "D"){ ($text)=&list_accounts($domain); &delete_account($text,$domain); #&text_box("Accounts",$text,"20 60"); } elsif($choice eq "F"){ # add FTP service... &services($ftp_file,"FTP"); } elsif($choice eq "M"){ # add MAIL service. &services("$pop_file","Mail"); } elsif($choice eq "H"){ # Help. I do plan on adding this soon..though, online help is available. my $text="No Help available yet!\nPlease read the online help\n at http://www.prongs.org/virtfs\n"; &text_box("HELP",$text,"12 60"); } elsif($choice eq "Q"){ my $text=`$chroot $domain $sendmail -bp`; &text_box("Mail Queue",$text,"20 60"); } elsif ($choice eq "B"){ my $text=`$chroot $domain $sendmail -bi`; &text_box("Alias Database",$text,"20 68"); } &menu_list; #back to listing the menu.. } sub change_passwd{ my ($text,$server)=@_; my ($acts,$i,$j); if ($passwd eq "none" ){&error("passwd not found!\nYou may need to edit this script!");} if ($chroot eq "none"){&error("chroot not found!\nYou may need to edit this script!");} my (@accounts)=split(/\n/,$text); for ($i=0;$i<@accounts;$i++){ $j=$i+1; $acts .= " $j \"$accounts[$i]\""; } &menu("Virtfs","\"Delete Accounts\"","20 70 11","$tmp/virt",$acts); my $choice=&get_answer("$tmp/virt"); $choice--; my $exec = "$chroot $server $passwd $accounts[$choice]"; my $status=system("$exec"); print "\nPress the enter key to re-enter virtfs\n"; my $reentry=; my $message; # in case of any errors $text = "Password for $accounts[$choice] changed.\n"; &info ($backtitle,$text,"10 50"); return; } sub mail_folder{ my ($text)=@_; my ($chown)=&find_path("chown"); if ($chown eq "none"){&error("chown not found!\nYou may need to edit this script!");} if ($chroot eq "none"){&error("chroot not found!\nYou may need to edit this script!");} my (@accounts)=split(/\n/,$text); my ($i,$acts,$j); for ($i=0;$i<@accounts;$i++){ $j=$i+1; $acts .= " $j \"$accounts[$i]\""; } &menu("Virtfs","\"Mail Folders\"","20 70 11","$tmp/virt",$acts); my $choice=&get_answer("$tmp/virt",3); if (!$choice){&menu_list;} $choice--; chomp ($accounts[$choice]); my $ex; if (! -d $domain.$mail_folder){ $ex = "$mail_folder was not found within domain.\n"; $ex .= "Edit $config_file. Are you running Sendmail?\n"; &text_box("Mail Folder","$ex","15 62"); return; } my $mailfile=$domain.$mail_folder."/".$accounts[$choice]; if (-e $mailfile){ $ex="Mail folder found:\n$mailfile ."; $ex .= "\nDelete and re-create the mail folder?"; my $del_folder=&yes_no("Mail Folder",$ex,"14 62"); if ($del_folder == 0){ unlink $mailfile; } else{return;} } open (FH, "+> $mailfile"); close (FH); $ex="Mail file $mailfile\ncreated."; $ex .= "\nPermissions were also passed.."; my $mailfolder=$mail_folder."/".$accounts[$choice]; chmod 0600, $mailfile || die "Couldn't change permissions of $mailfile: $!\n"; my $exec=`$chroot $domain $chown $accounts[$choice]:$mail_grp $mailfolder &1`; if ($?) {print $exec;exit(0);} &text_box("Mail Folder","$ex","15 62"); return; } sub finger_account{ my ($text)=@_; my ($finger)=&find_path("finger"); if ($finger eq "none"){&error("finger not found!\nYou may need to edit this script!");} if ($chroot eq "none"){&error("chroot not found!\nYou may need to edit this script!");} my (@accounts)=split(/\n/,$text); my ($i,$acts,$j); for ($i=0;$i<@accounts;$i++){ $j=$i+1; $acts .= " $j \"$accounts[$i]\""; } &menu("Virtfs","\"Finger Account\"","20 70 11","$tmp/virt",$acts); my $choice=&get_answer("$tmp/virt",3); if (!$choice){&menu_list;} $choice--; chomp ($accounts[$choice]); my $ex=`$chroot $domain $finger -l $accounts[$choice] &1`; &text_box("Finger",$ex,"21 62"); return 1; } sub delete_account{ my ($text,$server)=@_; if ($chroot eq "none"){&error("chroot not found!\nYou may need to edit this script!");} if ($userdel eq "none"){&error("userdel not found!\nYou may need to edit this script!");} my ($i,$acts,$j,$error_text); my (@accounts)=split(/\n/,$text); for ($i=0;$i<@accounts;$i++){ $j=$i+1; $acts .= " $j \"$accounts[$i]\""; } &menu("Virtfs","\"Delete Accounts\"","20 70 11","$tmp/virt",$acts); my $choice=&get_answer("$tmp/virt",3); if (!$choice){&menu_list;} if ($userdel eq "none" ){&error("userdel not found!\nYou may need to edit this script!");} $choice--; if (($accounts[$choice] eq "root") && ($delete_root == 1)){ $error_text = "The root account cannot be removed!\n"; &info ($backtitle,$error_text,"15 65"); return; } my ($exec,$ex); my $del_text = "Delete User's Home Directory?"; my $del_home=&yes_no("Home Dir!",$del_text,"14 62"); if ($del_home == 0){ $ex="$chroot $server $userdel -r $accounts[$choice]"; } else { $ex = "$chroot $server $userdel $accounts[$choice]";} my $ctext="Are you sure you would like to delete $accounts[$choice]"; $ctext .= "?" if ($del_home != 0); $ctext .= "\n"; $ctext .= "and the home dir contents?" if ($del_home == 0); my $confirm=&yes_no("Confirm",$ctext,"14 62"); if ($confirm == 0){ my $exec=`$ex &1`; $exec = "User $accounts[$choice] deleted." if (!$exec); &text_box("User Deleted",$exec,"20 60"); } &menu_list; } sub services{ my ($file,$service)=@_; my ($active)=&conf_files($file); my ($text,$dialog); if ($active eq "Yes"){ my $text = "The $service service is currently active.\nDisable It?"; my $dialog=&yes_no("Home Dir!",$text,"14 62"); if ($dialog == 0){ &delete_conf_entry($file); } } elsif ($active ne "Yes"){ $text = "The $service service is not active.\nEnable It?"; my $dialog=&yes_no("Home Dir!",$text,"14 62"); if ($dialog == 0){ &edit_conf_file($file); } } $active=&conf_files($file); $text = "$service service is currently active" if ($active eq "Yes"); $text = "$service service is currently not active" if ($active ne "Yes"); &info ($backtitle,$text,"10 50"); return; } sub su_exec{ # simly su over to the server as if you just telnetted into the virtual domain. my ($server)=@_; my $text = "Su over to $server as which user?\n"; $text .= "(Virtfs will exit)"; my ($su_user)=&input($backtitle,$text,"root","12 55"); if (!$su_user){&menu_list;} if ($su eq "none" ){&error("su not found!\nYou may need to edit this script!");return;} exec ("$chroot $server $su $su_user") || &error("$!"); exit(0); #in case } sub find_path{ # find the path of a given file for execution. my ($file)=@_; if (-e "$domain/usr/sbin/$file"){ return "/usr/sbin/$file"; } elsif (-e "$domain/bin/$file"){ return "/bin/$file";} elsif (-e "$domain/usr/bin/$file"){ return "/usr/bin/$file";} elsif (-e "$domain/usr/local/bin/$file"){ return "/usr/local/bin/$file";} elsif (-e "$domain/usr/lib/$file"){ return "/usr/lib/$file";} else { my $text .= "$file could not be found over on $domain\n"; $text .= "Make sure $file is in your PATH\n"; $text .= "Some things will not operate properly...\nContinue?"; my $continue=&yes_no("Error!",$text,"14 62"); if ($continue != 0){ exit(0); } else { return "none"; } } } sub add_user{ # Add a user to a domain. Similar to how virtexec would carry this out. # It will ask for the username, the name of the account and the home directory. my ($server)=@_; if ($adduser eq "none" ){&error("adduser not found!\nYou may need to edit this script!");} my $text = "Please enter the username:\n"; my ($user)=&input($backtitle,$text,"","12 55"); if (!$user){&menu_list;} $text = "What is the full name of $user\n"; my ($name)=&input($backtitle,$text,"","12 55",3); $text = "Home directory for $user (within $domainname):\n"; my ($dir)=&input($backtitle,$text,"$default_home/$user","12 55",3); $dir="$default_home/$user" if (!$dir); $text = "Shell for $user? Default will be $default_shell\n"; my ($shell)=&input($backtitle,$text,"$default_shell","12 55",3); #execute the command to su over as a user using '/' as the virtual directory. my $exec; if ($name){ $exec="$chroot $server $adduser -c \"$name\" -d $dir -s $shell $user"; } else{ $exec="$chroot $server $adduser -d $dir -s $shell $user"; } my $status=system("$exec 2>$tmp/user"); my $message; # in case of any errors if ($status != 0){ open (TEMP, "+< $tmp/user"); while (){ $message .= $_; # display the error to the end user, if any.. } close (TEMP); unlink ("$tmp/user"); &error("$message\n");} $text = "$user has been added to $domainname\n"; $text .= "You should now create a password and mail folder\nfrom the menu.\n"; &info ($backtitle,$text,"15 60"); } sub list_accounts{ my ($server)=@_; open (PD, "+< $server/etc/passwd") || &error ("No password file!\n"); my ($name,$text); while (){ ($name)=split(/\:/,$_); chomp ($name); $text .= "$name\n"; } close (PD); return $text; } sub list_users{ # Simply parse the passwd file of the domain and send it off to the # text file viewer. Display the Username, their home directory and the shell. my ($server)=@_; open (PD, "+< $server/etc/passwd") || &error ("No password file!\n"); my ($name, $dir,$uid,$gid,$quota,$comment,$gcos,$shell,$passwd,$text); $text = "Account Dir Shell\n"; $text .="----------------------------\n"; while (){ ($name,$passwd,$uid,$gid,$gcos,$dir,$shell)=split(/\:/,$_); chomp($shell); $text .= "$name - "; $text .= "$dir - " if ($dir); $text .= "[none] - " if(!$dir); $text .= "$shell\n" if ($shell); $text .= "[none]\n" if (!$shell); } close (PD); return $text; } sub perm_denied{ print "Sorry, but a configuration error occurred. Please check the virtfs configuration file...\n"; exit(0); } sub list_domains{ my ($virtual)=@_; # List all domains. We first check the virtfs configuration files, and # then the leading virtual directory for (unlisted) domains. #automatically add virtual domains to $server_file with $domain/etc entries my (@dirs,$name,$i,@domains,$list,$ip); open (FH,"+< $server_file") || &perm_denied; while (){ chomp; s/#.*//; s/^\s+//; s/\s+$//; next unless length; ($name,$ip)=split(/\:/, $_,2); $i++; $list .= "$name $ip " if $name && $ip && (-d "$virtual_dir/$name/etc"); # look for a filesystem } $list .= " Create Domain"; close (FH); # add directories! # &error("No Virtual Domains Found!\nMake sure configuration is okay...\n") if (!$list); &menu("Virtfs","Domains","20 70 12","$tmp/domain",$list); my $choice=&get_answer("$tmp/domain",1); open (FH,"+< $server_file") || &error ("Cannot open $server_file. Please make sure the configuration is alright: $!\n"); if ($choice ne "Create"){ while (){ next unless length; ($name,$ip)=split(/\s*\:\s*/,$_,2); if ($name eq $choice){ close (FH); return ($choice,$ip); } } } else { &create_virt_domain;exit(0);} close (FH); return ($choice); } sub radiolist{ # Give the end user a choice of radio buttons. my ($title,$subtitle,$size,$menu_file,$list)=@_; system ("dialog --clear --title $title --radiolist $subtitle $size $list 2> $menu_file"); return; } sub text_box{ # Display the text file viewer of a certain file. my ($title,$text,$size)=@_; open (TEMP, ">$tmp/text"); print TEMP $text; close (TEMP); system ("dialog --clear --title \"$title\" --textbox /tmp/text $size"); unlink "$tmp/text"; return; } sub error{ # Generic error message. Here the user is given two choices. To continue or not. my ($message)=@_; my $text .= "Error Found:\n$message\n"; $text .= "Continue?\n"; my $continue=&yes_no("Error!",$text,"12 53"); if ($continue==0){ my ($domain,$ip)=&list_domains($virtual_dir); &menu_list; } else{exit(0);} } sub list_menu{ # List the menu to the user and return the user's choice back to the main # part of the script. my ($domain)=@_; my $list = "I \"Server Information\""; $list .= " C \"Change Name or IP\""; $list .= " R \"Delete Server\""; $list .= " S \"su to Server\""; $list .= " F \"FTP Service\""; $list .= " M \"Mail Service\""; $list .= " A \"Add Account\""; $list .= " D \"Delete Account\""; $list .= " G \"Finger/Confirm Account\""; $list .= " U \"List Users\""; $list .= " P \"Change Password\""; $list .= " T \"Create Mail Folder\""; $list .= " Q \"View Sendmail Mail Queue\""; $list .= " B \"Rebuild Sendmail Aliases\""; $list .= " H \"Help\""; &menu("Virtfs","\"Menu For $domainname\"","20 70 11","$tmp/virt",$list); my $choice=&get_answer("$tmp/virt",1); return $choice; } sub menu{ my ($title,$subtitle,$size,$menu_file,$list)=@_; system ("dialog --clear --title $title --menu $subtitle $size $list 2> $menu_file"); return; } sub yes_no{ my ($title,$text,$size)=@_; if (!$size){ $size = "8 35"; } my $status=system ("dialog --backtitle \"Virtual Services - virtfs\" --yesno \"$text\" $size 2> $tmp/virt"); return $status; } sub input{ # This function outputs a Dialog box which takes in an input from the user # which we can later read in. my ($title,$subtitle,$text,$size,$ex)=@_; if ($size eq ""){ $size="9 35"; } unlink "$tmp/virt"; my $exec=system ("dialog $title --inputbox \"$subtitle\" $size \"$text\" 2> $tmp/virt"); if (($exec != 0) && ($ex)){ # user hit 'Cancel' so let's exit.... print "Exiting...\nThank you for using virtfs.\n"; exit(0); } elsif ($exec != 0){ #when cancel is pressed and we do not want to exit the program, as we do above. &menu_list; #return 0; } my ($choice)=&get_answer("",$ex); return $choice; } sub delete_conf_entry{ my ($conf)=@_; chomp($ip); chomp($domain); open (CONF, "+< $conf"); open (TEMP, "+> $tmp/conf"); my $format="$ip $domain"; while (){ print TEMP $_ unless $_ =~ /$format/; } close (CONF); close (TEMP); system ("mv $tmp/conf $conf"); return; } sub edit_conf_file{ my ($conf)=@_; chomp($ip); chomp($domain); my ($found); if (!-e $conf){ open (CONF, ">> $conf") || die "Couldn't create $conf\n"; print CONF "# Automatically written by Virtfs.\n"; print CONF "# Please don't edit this unless you know how to.\n"; print CONF "# ie. IP space DIRECTORY\n\n"; print CONF "$ip $domain\n"; print CONF "\ndefault /\n"; close (CONF); } else { open (CONF, "+< $conf"); unlink "$tmp/conf" if (-e "$tmp/conf"); my $temp="$tmp/conf"; my $format="$ip $domain"; open (TEMP, ">> $temp") || die "Couldn't open $temp!\n"; while (){ if (($_ =~ /default \//) || ($_ =~ /$format/)){$found=1;} else{ print TEMP $_ unless ($_ =~ /^$/); } } print TEMP "$format\n" if ($found); print TEMP "\ndefault /\n"; close (TEMP); system ("mv $temp $conf"); } } sub check_ip_format{ my ($ipaddr)=@_; return 1 if ($ipaddr =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/ && $1 >= 0 && $1 <= 255 && $2 >= 0 && $2 <= 255 && $3 >= 0 && $3 <= 255 && $4 >= 0 && $4 <= 255); return 0; } sub remove_server_entry{ my ($domain)=@_; open (SR, "+< $server_file"); open (TEMP, "+>>$tmp/domain.tmp"); while (){ print TEMP "$_" unless $_ =~ /$domainname\:$ip/; } close (TEMP); close (SR); system ("mv $tmp/domain.tmp $server_file"); return; } sub get_answer{ # This functions gets the user input from the dialog boxes. my ($file,$ex)=@_; if (!$file){$file = "$tmp/virt";} my ($choice); open (ANSWER, "$file"); while (){ chomp; $choice=$_; } close (ANSWER); if ((!$choice) && ($ex == 3)){ return; } #elsif ((!$choice) && ($ex == 5)){&menu_list;} elsif ((!$choice) && ($ex == 1)){ print "\nExiting...\n"; exit(0); } elsif (!$choice){ &menu_list; return; } return $choice; } sub write_up{ # This function writes in the domain-specific configuration stuff into a temp file. my ($key,$value)=@_; open (TEMP, "+>> $tmp/choices"); print TEMP "$key:$value\n"; close (TEMP); return; } sub info{ # this function simply builds the Info box from Dialog. my ($title,$text,$size)=@_; if (!$size){ $size="22 53"; } system("dialog $title --msgbox \"$text\" $size"); return; } sub lookfor{ my ($variable,$level)=@_; open (FH,$config_file) || &error ("Cannot open $config_file: $!\n"); my ($name,$value); while (){ chomp; s/#.*//; s/^s+//; s/\s+$//; next unless length; ($name,$value)=split(/\s*=\s*/, $_,2); if ($name eq $variable){ close (FH); return $value; } } close (FH); if ($level == 1){ &error("Couldn't read the configuration file\n$config_file for $variable!\n"); } } sub create_virt_domain{ unlink ("$tmp/choices"); my $domain=&get_virtual_dir; # specify all the directories needed &get_virtual_ip($domain); # And the IP address needed &write_up("copyfile",$config_file); my ($config)=&config_lookup; # Summarise what is going on to the user: my $text .= "This is the current configuration\n\n"; $text .= "Virtual Domain Directory: $config->{'virtdir'}\n"; $text .= "Domain Name: $config->{'domain'}\n"; $text .= "Domain IP Address: $config->{'virtip'}\n\n"; $text .= "\n\nShell Script File: $config->{'copyfile'}\n"; $text .= "\nShould we continue?\n"; my ($status)=&yes_no($backtitle,$text,"20 53"); if ($status != 0){ # user did not like what (s)he saw...leave... print "User did not like the configuration..Exiting...\n"; exit(0); } &execute($config); # Configuration is set, carry out operations ©_file($config); # copy all the files as read in by copyfile.txt return; } sub copy_file{ my ($config)=@_; my ($copy_files)=$config->{'copyfile'}; my $domain=$config->{'domain'}; my $text = "The files are about to be copied over.\n"; $text .= "This may take a while...Please be patient\n\n"; $text .= "A log will be kept in $tmp/$config->{'domain'}.\n\n"; $text .= "Please view that once the copying is done.\n"; $text .= "\nPressing Enter will begin the copying process\n"; &info($backtitle,$text,"15 50"); my (@ex,$i); open (FH, "+< $copy_files"); undef $/; @ex=split(/\n/,); for ($i=0;$i<@ex;$i++){ $ex[$i] =~ s/\#.*//; # cut out comments $ex[$i] =~ s/!!virtual/$config->{'virtdir'}/, $ex[$i]; $ex[$i] =~ s/!!domain/$config->{'domain'}/, $ex[$i]; #print "$ex[$i]\n"; system ("$ex[$i] 2>>$tmp/$domain"); } if (!-e "$config->{'mainvirtdir'}/servers.lists"){ open (RD, ">>$config->{'mainvirtdir'}/servers.lists"); print RD "# DO NOT TOUCH THIS FILE\n"; print RD "$config->{'domain'}:$config->{'virtip'}\n"; } else { open (RD, ">>$config->{'mainvirtdir'}/servers.lists"); print RD "$config->{'domain'}:$config->{'virtip'}\n"; } close (RD); $text = "Congratulations!\n"; $text .= "$config->{'domain'} has been setup.\n"; $text .= "\n\nYou will have to restart virtfs to configure"; &info($backtitle,$text,"15 50"); return; } sub execute{ #carry out the directory creation and pass on the variables my ($config)=@_; if (!-d $config->{'mainvirtdir'}){ mkdir $config->{'mainvirtdir'},0755 || die "Couldn't create directory!"; } mkdir $config->{'virtdir'},0755 || die "Couldn't create directory!"; } sub get_virtual_dir{ if (!-d $virtual_dir){ my $warningtext="The virtual directory $virtual_dir does not exist. Please enter in a default directory"; $warningtext .= " which will be created for you."; ($virtual_dir)=&input($backtitle,$warningtext,"$virtual_dir","15 55"); } my ($domain)=&input($backtitle,"Please Enter Domain Name (domain.com)","","15 55",1); if (-d "$virtual_dir/$domain"){ print "$domain seems to already exist! Try removing the server before trying to set it up again!\n"; exit(0); } &write_up("domain",$domain); &write_up("virtdir","$virtual_dir/$domain"); &write_up("mainvirtdir",$virtual_dir); return $domain; } sub get_virtual_ip{ my $ip; while ($ip !~ /\d+\.+\d+\.+\d+\.+\d+/){ # make sure it is an IP number entered by the user in the form of x.x.x.x ($ip)=&input($backtitle,"Please Enter The IP Address for $domain (digits only)","","15 55",1); } &write_up("virtip",$ip); # send to write it into the configuration file. return; } sub write_up{ # This function writes in the domain-specific configuration stuff into a temp file. my ($key,$value)=@_; open (TEMP, "+>> $tmp/choices"); print TEMP "$key:$value\n"; close (TEMP); return; } sub config_lookup{ # Read in all the user-specific configuration for what is going to be carried out. my ($file)="$tmp/choices"; my ($key,%config); open (FH, "+< $file"); while (){ chomp; my ($name,$value)=split(/\:/,$_); $config{$name}=$value; } return (\%config); }