Download the source: qtop_pl.gz
#!/usr/bin/perl # # qtop.pl (qmail to postfix) reads .qmail-* alias files and inputs them # into postfix's virtual file and aliases map file. # # # KNOWN PROBLEMS # This script cannot handle domains or aliases with "-" (e.g. some-alias@domain.com, alias@some-domain.com) # in them nor will it handle subdomains (sub.domain.com). The names of the alias files the script does not # process will be printed at the end of the script. # # # Todo # * output to the virtual file is shit, needs formatting # * add support for subdomains # * condense the code, a lot of unnecessary lines # * fix the known problems # # Written by Chad Thunberg from www.nologin.org # # SET THESE $vfile = "/etc/postfix/virtual"; $qalias = "/var/qmail/alias"; $tmpdir = "/tmp/qtop"; $aliasf = "/etc/mail/aliases"; # create the temp dir if(-e "$tmpdir") { die "$tmpdir exists, remove it first"; } mkdir("$tmpdir",0700) || die "cannot mkdir $tmpdir: $!"; # get a list of qmail alias files opendir(QALIAS, "$qalias") || die "$qalias doesn't exist: $!"; while ($name = readdir(QALIAS)) { # parses the domain and alias from the file name @file = split(/-/,$name); # if the name has no domain, then we assume its a local alias that belongs in # the alias map. if($#file == 1) { $alias = "$file[1]"; chomp($alias); open(NAME, "$qalias/$name") || die "can't open $qalias\/$name: $!"; @lines = <NAME>; close(NAME); chomp(@lines); $luser = join(", ", @lines); chomp(@lines); $luser = join(", ", @lines); open(ALIASF, ">>$aliasf") || die "can't open $aliasf: $!"; select(ALIASF); print("$alias: $luser\n"); close(ALIASF); } if($#file == 3) { $domain = "$file[1].$file[2]"; $alias = "$file[3]"; chomp($alias); open(NAME, "$qalias/$name") || die "can't open $qalias\/$name: $!"; @lines = <NAME>; close(NAME); chomp(@lines); $luser = join(", ", @lines); # Use a temporary file to store each domain's aliases before combining them # into the virtual files. We assume, if the file already exists, the initial # "domain anything" line is present if (! -e "$tmpdir\/$domain.tmp") { open(TMP, ">$tmpdir\/$domain.tmp") || die "can't create $domain.tmp: $!"; select(TMP); print("$domain\t\tanything\n"); close(TMP); } open(TMP, ">>$tmpdir\/$domain.tmp") || die "can't open $domain.tmp: $!"; if($alias eq default) { select(TMP); print("\@$domain\t\t$luser\n"); } else { select(TMP); print("$alias\@$domain\t\t$luser\n"); } close(TMP); } if($#file != 3 && $#file != 1) { select(STDOUT); print("The file $name was not processed\n"); } } closedir(QALIAS); # now that we have all our temporary files, input them into postfix's # virtual file opendir(TMPDIR, "$tmpdir") || die "$tmpdir doesn't exist: $!"; while ($name2 = readdir(TMPDIR)) { open(TMPFILE, "$tmpdir\/$name2") || die "can't open $tmpdir/$name2: $!"; @lines2 = <TMPFILE>; open(VFILE, ">>$vfile") || die "can't open $vfile: $!"; select(VFILE); print(@lines2); print("\n"); close(VFILE); close(TMPFILE); } closedir(TMPDIR); print("\n**** Don't forget to remake your aliases map, $aliasf ****\n");