#!/usr/bin/perl =head Convert vpopmail file system type aliases into mysql-style aliases similar to 'vconvert', but works on aliases instead of user accounts =synopsis Modify your database parameters (user,password,etc) in this script Also change the 'assign_file' if necessary Then just run it: # vconvert_alias =cut use DBI; use DBD::mysql; $domain = $ARGV[0] || ''; $dbhost = 'localhost'; $dbuser = 'root'; $dbpass = ''; $dbname = 'vpopmail'; $assign_file = '/var/qmail/users/assign'; die "Usage: $0 \n" if(! $domain); $dbh = DBI->connect("dbi:mysql:$dbname:$dbhost","$dbuser","$dbpass") || die "Database connection failed: $DBI::errstr\n"; ## Some prepared statements for searching and inserting records my $search = $dbh->prepare(" SELECT * FROM valias WHERE alias = ? AND domain = ? AND valias_line = ? "); my $insert = $dbh->prepare(" INSERT INTO valias (alias, domain, valias_line) values(?, ?, ?) "); print "Converting aliases from domain $domain\n\n"; ## Find the domain's directory from the assign file (why isn't this in the datbase?!?!) open(ASSIGN, "<$assign_file") || die "Unable to open assign file: $assign_file\n\n"; my $domain_dir = ''; while(my $line = ) { ## Not sure what the difference between the first and second field here is.... if($line =~ m/^\+.*?\-:$domain:/) { my $dir = (split(':', $line))[4]; ## Check to make sure the directory actually exists if(-d $dir) { $domain_dir = $dir; } } } close(ASSIGN); die "Unable to find domain directory\n\n" if(! $domain_dir); ## Found a directory, now look for .qmail- files in it opendir(DH, $domain_dir) || die "Unable to open domain dir: $domain_dir\n\n"; while(my $file = readdir(DH)) { ## Forward files are in the format ".qmail-USERNAME" if($file =~ m/^\.qmail-(.*)$/ && -f("$domain_dir/$file")) { $alias = $1; ## Catchalls are evil, skip them next if($alias eq 'default'); open(FORWARD, "<$domain_dir/$file") || die "Unable to open forward file $domain_dir/$file\n\n"; while(my $alias_line = ) { chomp($alias_line); printf("Found alias: %-30s => %-30s\n", "$alias\@$domain", $alias_line); ## Skip if it already exists in the database. Add it if not $search->execute($alias, $domain, $alias_line); if($row = $search->fetchrow_hashref) { print " Alias already exists in database\n"; } else { $insert->execute($alias, $domain, $alias_line); print " Inserted alias into database\n"; } } close(FORWARD); } } closedir(DH);