Download the source: addqalias_pl.gz

#!/usr/bin/perl # This script makes changes to qmail's control files # and alias files in order to add user's with virtual domains. # There is a lot of excess crap here that could be taken out # But I tried to make it as lamer proof as possible. # # Written by Chad Thunberg www.nologin.org # # Location of qmail files $rcpt_file = "/var/qmail/control/rcpthosts"; $virtdomain_file = "/var/qmail/control/virtualdomains"; $alias_dir = "/var/qmail/alias"; # functions called in program sub write_controls { open(RCPT_FILE, ">>$rcpt_file"); print RCPT_FILE "$domain\n"; close RCPT_FILE; open(VDOMAINS_FILE, ">>$virtdomain_file"); print VDOMAINS_FILE "$domain:alias-$fixed\n"; close VDOMAINS_FILE; } sub get_aliases { print STDOUT "\n"; print STDOUT "Enter the name of the first alias to be added (example: enter support if you want mail\n"; print STDOUT "for support\@$domain to go to one user): "; chop($alias = <STDIN>); while ($alias ne "done") { push @aliases, $alias; print STDOUT "Enter the email address or user name that $alias\@$domain will be forwarded to: "; chop($user = <STDIN>); push @users, $user; print STDOUT "Enter the next alias for $domain or type done: "; chop($alias = <STDIN>); } } sub write_aliases { $i = 0; foreach $alias (@aliases) { $user = $users[$i]; open(ALIAS_FILE, ">$alias_dir/.qmail-$fixed-$alias"); print ALIAS_FILE "$user\n"; close ALIAS_FILE; $i++; } } # Collects user input. print STDOUT "\n"; print STDOUT "----------------------------------------------------------------------------------\n"; print STDOUT "Don't worry about mistakes. I will give you the chance later to correct any input.\n"; print STDOUT "Enter a domain you wish to add (example: coolnet.net): "; chop($domain = <STDIN>); # Convert domain.com to domain-com for file creation. $fixed = $domain; $fixed =~ s/\./-/g; # Check to see if domain already is added. open (RCPT_FILE, $rcpt_file); chop(@RCPT_HOSTS = <RCPT_FILE>); close RCPT_FILE; foreach $host (@RCPT_HOSTS) { if ($host eq $domain) { print STDOUT "Warning: The domain, $domain, already exists!!!!\n"; print STDOUT "Do you want to make changes to current settings? (yes/no): "; chop($ans = <STDIN>); if ($ans eq "no") { print STDOUT "Quiting...\n"; exit 1; } elsif ($ans eq "n") { print STDOUT "Quiting...\n"; exit 1; } elsif ($ans eq "yes") { &get_aliases; $i = 0; foreach $alias (@aliases) { $user = $users[$i]; write STDOUT; $i++; } print STDOUT "\n"; print STDOUT "Does the above look correct? (yes/no): "; chop($ans = <STDIN>); if ($ans eq "yes") { &write_aliases; print STDOUT "Changes have been made\n"; exit 1; } elsif ($ans eq "y") { &write_aliases; print STDOUT "Changes have been made\n"; exit 1; } else { print STDOUT "Quiting...\n"; exit 0; } } elsif ($ans eq "y") { &get_aliases; $i = 0; foreach $alias (@aliases) { $user = $users[$i]; write STDOUT; $i++; } print STDOUT "\n"; print STDOUT "Does the above look correct? (yes/no): "; chop($ans = <STDIN>); if ($ans eq "yes") { &write_aliases; print STDOUT "Changes have been made\n"; exit 1; } elsif ($ans eq "y") { &write_aliases; print STDOUT "Changes have been made\n"; exit 1; } else { print STDOUT "Quiting...\n"; exit 0; } } } } print STDOUT "Do you want ALL email for $domain to go to one user? (yes/no): "; chop($ans =<STDIN>); if ($ans eq "yes") { print STDOUT "Enter the user name or email address that will recieve all email for $domain: "; chop($user = <STDIN>); print STDOUT "All email for $domain will be forwarded to $user, is this right? (yes/no): "; chop($ans = <STDIN>); if ($ans eq "yes") { &write_controls; open(ALIAS_FILE, ">$alias_dir/.qmail-$fixed-default"); print ALIAS_FILE "$user\n"; close ALIAS_FILE; } elsif ($ans eq "y") { &write_controls; open(ALIAS_FILE, ">$alias_dir/.qmail-$fixed-default"); print ALIAS_FILE "$user\n"; close ALIAS_FILE; } else { print STDOUT "Quiting...\n"; exit 0; } } elsif ($ans eq "y") { print STDOUT "Enter the user name or email address that will recieve all email for $domain: "; chop($user = <STDIN>); print STDOUT "All email for $domain will be forwarded to $user, is this right? (yes/no): "; chop($ans = <STDIN>); if ($ans eq "yes") { &write_controls; open(ALIAS_FILE, ">$alias_dir/.qmail-$fixed-default"); print ALIAS_FILE "$user\n"; close ALIAS_FILE; } elsif ($ans eq "y") { &write_controls; open(ALIAS_FILE, ">$alias_dir/.qmail-$fixed-default"); print ALIAS_FILE "$user\n"; close ALIAS_FILE; } else { print STDOUT "Quiting...\n"; exit 0; } } else { &get_aliases; $i = 0; foreach $alias (@aliases) { $user = $users[$i]; write STDOUT; $i++; } print "\n"; print STDOUT "Does the above look correct? (yes/no): "; chop($ans = <STDIN>); if ($ans eq "yes") { &write_aliases; &write_controls; print STDOUT "Changes have been made\n"; } elsif ($ans eq "y") { &write_aliases; &write_controls; print STDOUT "Changes have been made\n"; } else { print STDOUT "Quiting...\n"; exit 0; } } kill 'HUP', `/bin/ps -a | /usr/bin/grep qmail-send | /usr/bin/grep -v grep | /usr/bin/cut -d" " -f1`; print STDOUT "Qmail has been restarted so the changes should now be active\n"; print STDOUT "Congrats, you are done.\n"; # format templates format STDOUT_TOP = Summary Aliases Forwarded to -------------------------------------------------------------------------- . format STDOUT = @>>>>>>>>>>>>>@@<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $alias,"@",$domain, $user .