#!/usr/bin/perl #use strict; # # maildomain creates, modifies, and deletes mail domains on a Matt style mail toaster # # Description: A handy script that add's domains to a server farm and # pushes the changes out to each of the servers. # # Expectations: # The vpopmail package from http://www.inter7.com/vpopmail # A mechanism to distribute configuration files (rdist/rsync) # A properly configured /etc/pw.conf file # Properly configured and enabled file system quotas # Setquota (/usr/ports/sysutils/setquota) # .qmailadmin-limits file configured with settings appropriate for your site # # Changelog # 0.9.4 - Nov 2, 2000 - When deleting domains, allow removal of # all domains for a user or just a single domain. # 1.0 - Nov 3, 2000 - Finished security settings, sudo intregration. # 1.1 - Nov 4, 2000 - Added show features, extended documentation. # 1.1.2 - Nov 6, 2000 - Additional error checking added. (verify user added). # Update permissions on new mail directories so techsupport # user can glob directory. # 1.2 - Nov 6, 2000 - Added ability to have virtual domains that are created with # the same password of the "master" domain for the given user. # 1.2.1 - Nov 7 - Modified main domain to store info in gecos field of system password # file instead of a .maindomain file in user dir. (thanks John Dindot) # Fixed a bug that caused vdomain quotas to not get set. # 1.2.3 - Nov 15 - Added more debugging (make sure vadddomain didn't fail). # 1.2.6 - Nov 29 - Err out if domain name isn't give to us in all lower case. # 1.3.1 - Jan 10, 01 - Bump up file system hard quota 5MB larger than vpopmails. # 1.3.2 - Jan 15, 01 - Added the padd (add domain pointer) feature. # 1.4 - Jan 25, 01 - Added vpasswd recovery feature, incorporated homedir tree # creation function. # 1.4.1 - Feb 21, 01 - Added switch for single or multiple uid's # Added adduser feature, config switch for home dir tree. # 1.4.2 - Feb 21, 01 - Added create_sys_user switch for creation of home dirs # 1.4.3 - Mar 03, 01 - Added ability to edit qmailadmin limits. # 1.4.4 - Mar 04, 01 - Added flags for disabling the vpassrestore and show features. # 1.4.5 - Mar 12, 01 - Added a "hold" feature and checking for duplicate domain names. # 1.4.6 - Mar 13, 01 - Corrected a problem with the padd feature not returning the correct # result code. $version = "maildomain version 1.5 by Matt Simerson 3.16.2001\n\n"; $update = "/usr/local/sbin/update"; # custom script that pushes config files to clients $home = "/usr/home"; $tar = "/usr/bin/tar"; $gzip = "/usr/bin/gzip"; $rm = "/bin/rm"; $rmdir = "/bin/rm -rf"; $cp = "/bin/cp"; $mkdir = "/bin/mkdir -p"; $pw = "/usr/sbin/pw"; $chown = "/usr/sbin/chown"; $chmod = "/bin/chmod"; $quota = "/usr/bin/quota"; $grep = "/usr/bin/grep"; $setquota = "/usr/local/sbin/setquota"; $vadddomain = "/usr/local/vpopmail/bin/vadddomain"; $vaddaliasdomain = "/usr/local/vpopmail/bin/vaddaliasdomain"; $vadduser = "/usr/local/vpopmail/bin/vadduser"; $vmoduser = "/usr/local/vpopmail/bin/vmoduser"; $vdeldomain = "/usr/local/vpopmail/bin/vdeldomain"; $vmkpasswd = "/usr/local/vpopmail/bin/vmkpasswd"; $vpasswd = "/usr/local/vpopmail/bin/vpasswd"; $vsetuserquota = "/usr/local/vpopmail/bin/vsetuserquota"; $vinfo = "/usr/local/vpopmail/bin/vuserinfo"; $qmailadminlimits = "/var/qmail/control/.qmailadmin-limits"; # Use multiple UID's or a single UID? $singleuid=""; # If all your mail domains will be owned by a single user #$singleuid="vpopmail"; # then set to the username of your vpopmail user # Create a system user? $create_sys_user="y"; # by default, when in single uid mode this script won't #$create_sys_user="n"; # create a new system user. This flag overrides that. (y/n) # Utilizing a homedir tree? (/usr/home/a/ab/abcuser instead of /usr/home/abcuser) $homedirtree="y"; # Use home dir tree - set to (y/n). # If using a system user account, where do we put their home dir? $homedir_base="u"; # Make the home dir the username or the domain name (u/d) # Do we want the users to be able to log in interactively (telnet/ssh)? $use_passwords="n"; # Put the postmaster password into the system passwd files # Misc stuff you may or may not want to use. $vpassrestore="y"; $show_function="y"; ######################################################################################## # There should be no need to modify anything below this line # ######################################################################################## # Check to see if we're running as root. If we aren't (and # usually won't be) then we need to prepend all our commands # with the sudo command. If we're running as a priviliged # user then sudo will execute our commands with root priviliges. # Of course, this does require that sudo be installed and configured # if ( $< ne "0" ) { $sudo = "/usr/local/bin/sudo"; } else { $sudo = ""; }; # Now we need to see if the script was passed any parameters and complain # if not. If we have all of the parameters we need defined we'll # execute the appropriate subroutine that does the actual work. # if ($ARGV[0] eq "add" or $ARGV[0] eq "vadd") { if ( $singleuid eq "" || ( $singleuid ne "" && $create_sys_user eq "y" )) { if ($ARGV[4] > 0 ) { add_domain( $ARGV[0], $ARGV[1], $ARGV[2], $ARGV[3], $ARGV[4], $ARGV[5] ); } else { print $version; print "\nI need you to pass me a command like this:\n\n"; print "maildomain vadd \n\n"; }; } else { if ($ARGV[3] > 0 ) { add_domain( $ARGV[0], $ARGV[1], $ARGV[2], $ARGV[3], $ARGV[4] ); } else { print $version; print "\nI need you to pass me a command like this:\n\n"; print "maildomain add \n\n"; }; }; } elsif ( $ARGV[0] eq "padd" ) { if ($ARGV[2] ne "" ) { add_domain( $ARGV[0], $ARGV[1], $ARGV[2] ); } else { print $version; print "\nI need you to pass me a command like this:\n\n"; print "maildomain padd \n\n"; }; } elsif ( $ARGV[0] eq "hold" || $ARGV[0] eq "unhold" ) { if ( $ARGV[0] eq "hold" && $ARGV[1] ne "" ) { hold( $ARGV[1] ); } elsif ( $ARGV[0] eq "unhold" && $ARGV[1] ne "" ) { unhold( $ARGV[1] ); } else { print $version; print "\nI need to to pass me a command like this:\n\n"; print "maildomain [un]hold \n\n"; print "\n"; }; } elsif ( $ARGV[0] eq "suspend" ) { if ( $ARGV[1] ne "" ) { suspend( $ARGV[1] ); } else { print $version; print "\nI need to to pass me a command like this:\n\n"; print "maildomain suspend \n\n"; print "\n"; }; } elsif ( $ARGV[0] eq "restore" ) { if ( $ARGV[1] ne "" ) { restore( $ARGV[1] ); } else { print $version; print "\nI need you to pass me a command like this:\n\n"; print "maildomain restore \n\n"; }; } elsif ( $ARGV[0] eq "vpassrestore" ) { if ( $ARGV[1] ne "" ) { vpassrestore( $ARGV[1], $ARGV[2] ); } else { print $version; print "\nI need you to pass me a command like this:\n\n"; print "maildomain vpassrestore \n\n"; }; } elsif ( $ARGV[0] eq "modify" ) { if ( $ARGV[1] ne "" ) { modify( $ARGV[1], $ARGV[2], $ARGV[3], $ARGV[4] ); } else { print $version; print "\nI need you to pass me a command like this:\n\n"; print " maildomain modify password \n"; print " or maildomain modify quota main \n"; print " or maildomain modify quota domain \n"; print " or maildomain modify quota user \n"; print " or maildomain modify limits domain \n\n"; }; } elsif ( $ARGV[0] eq "delete") { if ( $singleuid eq "" || ( $singleuid ne "" && $create_sys_user eq "y" )) { if ( $ARGV[1] eq "domain" and $ARGV[2] ne "" ) { delete_domain( $ARGV[2] ); print "+100 deldomain Done.\n\n"; } elsif ( $ARGV[1] eq "user" and $ARGV[2] ne "" ) { delete_domain( get_domain_list( $ARGV[2] ) ); delete_user( $ARGV[2], $ARGV[3] ); } else { print $version; print "\nI need you to pass me a command like this:\n\n"; print " maildomain delete domain \n"; print " or maildomain delete user [archive]\n\n"; print "To archive the domain files pass any non-zero value with the"; print " delete switch.\n\n"; }; } else { if ( $ARGV[1] ne "" ) { delete_domain( $ARGV[1] ); print "+100 deldomain Done.\n\n"; } else { print $version; print "\nI need you to pass me a command like this:\n\n"; print " maildomain delete \n\n"; }; }; } elsif ( $ARGV[0] eq "setup" ) { print $version; make_home_dir_tree(); } elsif ( $ARGV[0] eq "adduser") { if ( $ARGV[1] ne "" ) { system "$vadduser $ARGV[1] $ARGV[2]"; print "+100 useradd Done.\n\n"; } else { print $version; print "\n I need a user\@domain.com handed to me.\n\n"; }; } elsif ( $ARGV[0] eq "show" ) { show_mail_settings( $ARGV[1], $ARGV[2], $ARGV[3] ); } else { print $version; print "\n Valid choices are: \n\n"; if ( $singleuid eq "" ) { print " add - creates a new user & mail domain\n"; print " vadd - add a new mail domain to an existing user\n"; } else { print " add - creates a new mail domain\n"; }; print " padd - add a mail domain pointer\n\n"; print " hold - Disable an accounts ability to retrieve email\n"; print " unhold - Enable an accounts ability to retrieve email\n"; if ( $singleuid eq "" ) { print " suspend - suspend a user and all their mail domains\n"; print " restore - restore a suspended user and their domains\n"; }; if ( $vpassrestore eq "y" ) { print " vpassrestore - restore the vpasswd file of a domain\n\n"; }; if ( $show_function eq "y" ) { print " show - display properties of a user or domain\n"; }; print " adduser - add a user to a maildomain\n"; print " modify - make changes to a user or domain\n"; if ( $singleuid eq "" ) { print " delete - remove a mail user or domain(s)\n\n"; } else { print " delete - remove a mail domain\n\n"; }; }; # Adding a domain # sub add_domain { print $version; if ( $_[0] eq "add") { if ( $singleuid eq "" || ( $singleuid ne "" && $create_sys_user eq "y" )) { if ( getpwnam($_[1]) > 0 ) { # Check to make sure they don't exist die " FAILED: Uh-oh, that user is already in the passwd file.\n\n"; } elsif ( $_[1] ne lc $_[1] ) { die " FAILED: username must be all lower case!\n\n"; } else { if ( $homedir_base eq "d" ) { set_home_dir( $_[2] ); } else { set_home_dir( $_[1] ); }; print " First we add $_[1] to the system password files..."; if ( $use_passwords eq "y" ) { open( FH, "|$sudo $pw useradd -n $_[1] -d $homedir -c $_[2] -g vchkpw -h 0 -m"); print FH $_[3],"\n"; close (FH); } else { system "$sudo $pw useradd -n $_[1] -d $homedir -c $_[2] -g vchkpw -h- -m"; }; if ( getpwnam($_[1]) > 0 ) { # Check to make the user got added properly system "$sudo $update passwd"; # If so, push changes to mail servers print "done.\n\n Then we'll set their quota to $_[4] megabytes..."; $quotabump = $_[4] + 5; system "$sudo $setquota -u -f /usr -bh${quotabump}M -bs${_[4]}M $_[1]"; # system "$sudo $chmod 775 $homedir"; print "done.\n\n"; } else { die "FAILED.\n\nThe user failed to be added. This must be fixed before I'll resume.\n\n"; }; }; } else { if ( getpwnam( $singleuid ) > 0 ) { # Make sure user exists if ( $homedir_base eq "d" ) { set_home_dir( $_[2] ); } else { set_home_dir( $singleuid ); }; } else { die " FAILED: The user $singleuid does not exist!\n\n"; }; }; } elsif ( $_[0] eq "vadd" ) { if ( getpwnam($_[1]) > 0 ) { # Make sure user exists if ( $homedir_base eq "d" ) { set_home_dir( $_[2] ); } else { set_home_dir( $_[1] ); }; } else { die " FAILED: The user must exist to use the vadd feature.\n\n"; }; } elsif ( $_[0] eq "padd" ) { if ( does_domain_exist( $_[2] ) eq "1" ) { print " Creating alias for $_[1] to $_[2]..."; system "$sudo $vaddaliasdomain $_[1] $_[2]"; } else { die " The domain $_[2] must exist to use padd!\n\n"; }; } else { die " FAILED: something bad happened"; }; if ( $_[0] eq "add" or $_[0] eq "vadd" ) { if ( does_domain_exist( $_[2] ) eq "0" ) { print " And then set up the domain $_[2]..."; if ( $singleuid eq "" ) { system "$sudo $vadddomain -u $_[1] -q ${_[4]}000000 $_[2] $_[3]"; system "$sudo $chmod 750 $homedir/domains $homedir/domains/$_[2]"; @domain_list=get_domain_list( $_[1] ); # make sure the domain got created } elsif ( $singleuid ne "" && $create_sys_user eq "y" ) { system "$sudo $vadddomain -u $singleuid -q ${_[4]}000000 $_[2] $_[3]"; } else { system "$sudo $vadddomain -u $singleuid -q ${_[3]}000000 $_[1] $_[2]"; @domain_list=get_domain_list( $singleuid ); # make sure the domain got created }; if ( $_[0] eq "vadd" and $_[3] eq "null" ) { # Check the password for the keyword "null" get_main_domain( $_[1] ); print "done.\n\n... Setting password for $_[2] to same as $maindomain...."; # print "the password for $maindomain is $maindomainpass.\n"; set_pass_to_main( $homedir, $_[2], $maindomainpass ); system "$sudo $vmkpasswd $_[2]"; }; if ( $_[5] gt 0 ) { system "$sudo $cp $qmailadminlimits $homedir/domains/$_[2]/"; system "$sudo $chown $_[1] $homedir/domains/$_[2]/.qmailadmin-limits"; }; } else { print "That domain already exists! If you get this error something is messed up.\n"; die "Please report what caused this failure to mpsimerson\@hostpro.com\n\n"; }; print "done.\n\n Finally, we'll tell the mail servers about it..."; system "$sudo $update mail"; print "+100 adddomain Done.\n\n"; } elsif ( $_[0] eq "padd" ) { print "+100 padddomain Done.\n\n"; }; }; # Holding a domain # # To hold a domain, we simply remote the users ability to retrieve their mail. # sub hold { print $version; print "\n\n We'll remove their ability to retrieve their email..."; @domains = get_domain_list( $_[0] ); # print "\narray domains is: @domains\n"; foreach $domain ( @domains ) { # print "\ndomain is: $domain\n"; $domaindir = get_domains_homedir( $domain ); # print "domaindir is: $domaindir\n"; @users = get_userlist_for_domain( $domaindir ); # print "array users is: @users\n"; foreach $user ( @users ) { system "$sudo $vmoduser -i -p -w $user\@$domain"; # print "$sudo $vmoduser -i -p -w $user\@$domain\n"; }; }; print "done.\n\n"; print "+100 hold/unhold Done.\n\n"; }; sub unhold { print $version; print "\n\n We'll enable their ability to retrieve their email..."; @domains = get_domain_list( $_[0] ); foreach $domain ( @domains ) { $domaindir = get_domains_homedir( $domain ); @users = get_userlist_for_domain( $domaindir ); foreach $user ( @users ) { system "$sudo $vmoduser -x $user\@$domain"; }; }; print "done.\n\n"; print "+100 hold/unhold Done.\n\n"; }; sub get_userlist_for_domain { # print "vpassword file is: $_[0]/vpasswd\n"; open VPASSWD, "$_[0]/vpasswd"; @lines = ; close VPASSWD; foreach $line ( @lines ) { chomp $line; # print "variable line is: $line\n"; @fields = split(/:/, $line ); # print "array fields is: $fields[0] $fields[1] $fields[2] $fields[3] $fields[4]\n"; push @users, $fields[0]; }; return @users; }; # Suspending a domain # # The mail server has two parameters it needs to find a user, the domain # mapping and the system user. To suspend a domain, we simply remove the # mapping of the domain. sub suspend { print $version; print "\n\n First we want to back up $_[0]'s files...\n\n"; archive_user( $_[0] ); print "\n\n Then we'll remove the domains from the config files..."; get_domain_list( $_[0] ); foreach $domain ( @_ ) { system "$sudo $vdeldomain $domain"; }; print "done.\n\n And then tell the mail servers about it..."; system "$sudo $update mail"; print "done.\n\n"; }; # Restore a domain sub restore { if ( getpwnam($_[0]) > 0 ) { # Make sure user exists print $version; set_home_dir( $_[0] ); if ( -e "${homedir}.tar.gz" ) { print "done.\n\n First extract all their files..."; system "$sudo $tar -xvzf ${homedir}.tar.gz"; # unlink ${homedir}.tar.gz; # Delete the archive. system "$sudo $rm ${homedir}.tar.gz"; # Delete the archive. print " Restoring $_[0]'s domains...."; get_domain_list( $_[0] ); foreach $domain (@domain_list) { system "$sudo $vadddomain -u $_[0] $domain junk"; }; print "done.\n\n Finally, we'll tell the mail servers about it..."; system "$sudo $update mail"; print "done.\n\n"; } else { die "failed: ${homedir}.tar.gz does not exist.\n\n"; }; } else { die " FAILED: The user $_[0] doesn't exist.\n\n"; }; }; # Restore a domains vpasswd file. sub vpassrestore { $userdir = get_users_homedir( $_[0] ); opendir DOMAINDIR, "$userdir/domains/$_[1]" or die "FAILED: Can't read $userdir/domains: $!\n"; @dir_list = grep !/^\.\.?$/, readdir DOMAINDIR; # Grab a list of their user dirs closedir DOMAINDIR; $file = "/tmp/$_[1]"; $file2 = "$userdir/domains/$_[1]/vpasswd"; open NEWVPASS, ">$file"; $counter = 0; foreach $user ( @dir_list ) { if ( $user ne "vpasswd" && $user ne "vpasswd.cdb" && not ( $user =~ /^\./ )) { print NEWVPASS "$user:nWCF8/.cJUZ5.:1:0:$user:$userdir/domains/$_[1]/$user:100000000\n"; }; ++$counter; }; close NEWVPASS; system "$sudo $cp $file $file2"; system "$sudo $vmkpasswd $_[1]"; system "$sudo $rm $file"; print "\n\nOK, the vpasswd file lives in $file2n\n"; print " The default passwords have been reset to 'temp'\n"; }; # Modify a domain # # The modify command can accept one of two options: quota, or password. # We expect the modify command to be passed to us with one of those # keywords and a value to set. sub modify { if ( $_[0] eq "quota" && $_[1] eq "main" && $_[3] ne "" ) { print $version; print " \nModifying $_[2]'s quota to $_[3] megabytes...."; system "$sudo $setquota -u -f /usr -bh${_[3]}M -bs${_[3]}M $_[2]"; get_main_domain( $_[2] ); print "main domain is: $maindomain\n"; system "$sudo $vsetuserquota postmaster\@${maindomain} ${_[3]}000000"; print "completed.\n\n"; } elsif ( $_[0] eq "quota" && $_[1] eq "domain" && $_[3] ne "" ) { print $version; print " \nModifying postmaster\@${_[2]}'s quota to $_[3] megabytes...."; system "$sudo $vsetuserquota postmaster\@$_[2] ${_[3]}000000"; print "completed.\n\n"; } elsif ( $_[0] eq "quota" && $_[1] eq "user" && $_[3] ne "" ) { print $version; print " \nModifying ${_[2]}'s quota to $_[3] megabytes...."; system "$sudo $vsetuserquota ${_[2]} ${_[3]}000000"; print "completed.\n\n"; } elsif ( $_[0] eq "password" && $_[2] ne "" ) { print $version; print " \nModifying $_[1]'s password to $_[2]..."; $cmd = "$sudo $vpasswd $_[1] $_[2]"; # print "\n\nthe value of string command is: ${cmd}\n\n"; system $cmd; print "done.\n\n"; } elsif ( $_[0] eq "limits" && $_[2] ne "" ) { print $version; edit_qmailadmin_limits($_[0], $_[1], $_[2]); } else { print $version; print " \nDid you pass your command the right number of parameters? Ex:\n\n"; print " maildomain modify password user\@domain.com password\n\n"; print " maildomain modify quota main \n"; print " maildomain modify quota domain \n"; print " maildomain modify quota user \n\n"; print " or maildomain modify limits domain \n\n"; }; }; # Delete a domain # # The delete domain script can (optionally) be called with any non-zero value # and if so, it will first archive the domain home directory before blowing it # away. The pw utility does not, by default, remove the domain home directory # so we pass it the "-r" flag. # sub delete_user { if ( getpwnam($_[0]) > 0 ) { # Make sure user exists print $version; if ( $_[1] ne "" ) { # Check for archive flag archive_user( $_[0] ); }; get_domain_list( $_[0] ); # Find all domains for user delete_domain( @domain_list ); # Remove them from qmail assigns print " Deleting $_[0]'s home directory...."; system "$sudo $pw userdel -n $_[0] -r"; print "done.\n\n Now we'll tell all the mail servers about it..."; system "$sudo $update passwd"; print "+100 deldomain Done.\n\n"; } else { print $version; die " FAILED: That user doesn't exist.\n"; }; }; sub archive_user { set_home_dir( $_[0] ); print " Archiving file to $homedir.tar...."; system "$sudo $tar -Pcf ${homedir}.tar $homedir"; print "done.\n\n Compressing archive file to $homedir.tar.gz...."; system "$sudo $gzip ${homedir}.tar"; if ( -e "${homedir}.tar.gz" ) { print "done.\n\n"; } else { die "failed: couldn't complete archiving of $homedir.tar.gz.\n\n"; }; }; sub delete_domain { print $version; foreach $domain ( @_ ) { print " Removing the domain $domain from the config files..."; system "$sudo $vdeldomain $domain"; print "done.\n"; }; system "$sudo $update mail"; }; sub set_home_dir { if ( $homedirtree eq "y" ) { $tmp1 = substr($_[0], 0, 1); # Grab the first char of their login $tmp2 = substr($_[0], 0, 2); # Grab the first two characters $homedir = "$home/$tmp1/$tmp2/$_[0]"; # Define their home directory } else { $homedir = "$home/$_[0]"; }; return $homedir; }; sub get_domain_list { @password=getpwnam( $_[0] ); # Fetch their system passwd info # @dir_list=<$password[7]/domains/[a-z]*> or die "FAILED: Can't read $password[7]/domains: $!"; # Grab a list of their domains dirs # opendir DOMAINDIR, "$password[7]/domains"; opendir DOMAINDIR, "$password[7]/domains" or die "FAILED: Can't read $password[7]/domains: $!\n"; @dir_list = grep !/^\.\.?$/, readdir DOMAINDIR; # Grab a list of their domains dirs closedir DOMAINDIR; # print @dir_list; $counter = 0; foreach $domain ( @dir_list ) { @parts=split("/", $dir_list[$counter]); # Strip off the path @domain_list[$counter]=$parts[$#parts]; # Add it to their list of domains ++$counter; }; return @domain_list; }; sub show_mail_settings { if ( $_[0] eq "quota" ) { print $version; system "$sudo $quota $_[1]"; print "\n\n"; get_domain_list( $_[1] ); foreach $domain ( @domain_list ) { print "\n ********* domain quota for $domain (in bytes) *********\n"; system "$sudo $vinfo -q postmaster\@$domain"; }; print "\n"; } elsif ( $_[0] eq "userquota" ) { print $version; system "$sudo $vinfo -q $_[1]"; print "\n"; } elsif ( $_[0] eq "all" ) { print $version; system "$sudo $quota $_[1]"; print "\n\n"; get_main_domain( $_[1] ); print " ********* main domain is $maindomain *********\n"; get_domain_list( $_[1] ); foreach $domain ( @domain_list ) { print "\n ********* info for $domain *********\n"; system "$sudo $vinfo postmaster\@$domain"; }; } else { print $version; print "\n Valid choices are: \n\n"; print " quota - Display quota for a user account and their domains (user)\n"; print " userquota - Display quotas for an email account (user\@domain.com)\n"; print " all - Display all info for a user and their domains\n\n"; }; }; sub get_main_domain { @password=getpwnam( $_[0] ); # Fetch their system passwd info # print "the password fields for $_[0] are: @password"; $maindomain=$password[6]; # print $maindomain, "\n"; open( PASS, "<$password[7]/domains/$maindomain/vpasswd" ); foreach $line () { # print $line; @fields = split(":", $line); if ( $fields[0] eq "postmaster" ) { $maindomainpass=$fields[1]; }; }; close( PASS ); # print "the crypted main domain pass is $maindomainpass.\n"; }; sub set_pass_to_main { # print "opening file $_[0]/domains/$_[1]/vpasswd.\n"; open( PASS, "+<$_[0]/domains/$_[1]/vpasswd" ); $counter = 0; foreach $line ( ) { @fields = split( ":", $line ); if ( $fields[0] eq "postmaster" ) { # print "inserting $_[2] into the password field.\n"; $fields[1] = $_[2]; $vpasswd[$counter] = join(":", @fields); } else { $vpasswd[$counter] = $line; }; # print $vpasswd[$counter]; ++$counter; }; close( PASS ); open( PASS, ">$_[0]/domains/$_[1]/vpasswd" ); foreach $line ( @vpasswd ) { print PASS $line; }; close( PASS ); # print @vpasswd; }; sub does_domain_exist { $assign =`$grep "+${_[0]}" /var/qmail/users/assign`; @assign_parts = split( /:/, $assign ); # print "\n\n $assign \n\n $assign_parts[1] \n\n"; if ( $assign_parts[1] eq "$_[0]" ) { return "1"; } else { return "0"; }; }; sub make_home_dir_tree { @alpha = qw(a b c d e f g h i j k l m n o p q r s t u v w x y z); @num = qw( 1 2 3 4 5 6 7 8 9 0 ); foreach $iter (@alpha) { foreach $iter2 (@alpha) { print "creating directory /usr/home/${iter}/${iter}${iter2}\n"; system "$mkdir /usr/home/${iter}/${iter}${iter2}"; }; foreach $iter2 ( @num ) { print "creating directory /usr/home/${iter}/${iter}${iter2}\n"; system "$mkdir /usr/home/${iter}/${iter}${iter2}"; }; }; }; sub edit_qmailadmin_limits { print " Do, you want to edit $_[0] limits? (y/n): "; $yes_or_no = ; chomp $yes_or_no; $yes_or_no = lc( $yes_or_no ); if ( $yes_or_no eq "y" ) { print "\n\t\t0 = disabled and empty is unlimited\n"; print "\n Please specify the maxiumum number of POP accounts: "; $maxpop = ; chomp $maxpop; print " Please specify the maxiumum number of aliases: "; $maxalias = ; chomp $maxalias; print " Please specify the maxiumum number of forwards: "; $maxforward = ; chomp $maxforward; print " Please specify the maxiumum number of mailing lists: "; $maxlist = ; chomp $maxlist; print " Please specify the maxiumum number of autoresponders: "; $maxauto = ; chomp $maxauto; print "\n\n\tmaxpopaccounts\t\t$maxpop\n\tmaxaliases\t\t$maxalias\n\tmaxforwards\t\t$maxforward\n\tmaxmailinglists\t\t$maxlist\n\tmaxautoresponders\t$maxauto\n"; print "\n\n Your file looks like this, go ahead and install? : "; $install = ; chomp $install; if ( lc($install) eq "y" ) { $homedir = get_domains_homedir($_[2]); print "\nInstalling file $homedir/.qmailadmin-limits..."; open ( LIMITS, ">/tmp/.qmailadmin-limits" ); print LIMITS "maxpopaccounts\t\t$maxpop\nmaxaliases\t\t$maxalias\nmaxforwards\t\t$maxforward\nmaxmailinglists\t\t$maxlist\nmaxautoresponders\t$maxauto\n"; close LIMITS; system "$sudo cp /tmp/.qmailadmin-limits $homedir\n"; system "$sudo chown vpopmail.vchkpw $homedir/.qmailadmin-limits\n"; unlink /tmp/.qmailadmin-limits; print "done.\n"; }; } else { print " Oh well, see ya later\n"; }; }; sub get_domains_homedir { print "I'm searching for: $_[0]\n"; @lines = `grep +${_[0]} /var/qmail/users/assign`; $rows = @lines; if ( $rows > 1 ) { print "Oh crap, you've got duplicate entries for ${_}. You need to fix that!\n"; print @lines; } elsif ( $rows < 1 ) { print "Sorry, ${_} doesn't exist\n"; print @lines; } else { @fields = split(/:/, $lines[0]); # print @lines; return $fields[4]; }; }; sub get_users_homedir { # print " First we need to figure out $_[0]'s home directory\n"; @password=getpwnam( $_[0] ); # Fetch their system passwd info # print " which is: $password[7]\n\n"; return $password[7]; };