Автор Гілка: Dovecot.passwd  (Прочитано 2162 раз)

Praporshic

  • Гість
Dovecot.passwd
« : 2006-12-01 13:33:30 »
Довелось нещодавно переводити поштову систему на використання віртуальних користувачів з базою у passwd-file.
В наслідок цього виник невеличкий сценарій, може комусь стане у пригоді:
#!/usr/bin/perl
#################################################################################
#dovecot.passwd creator
#Create dovecot password file for exist system users.
#Used for extended authentification (login@domain)
#Required arguments: dovecot.passwd file path, localhost name
#################################################################################

$file = @ARGV[0];
$hostname = @ARGV[1];

die "Usage: ./dovecot_pwfile_gen.pl [path_to_file] hostname" unless @ARGV;

unless (!-e $hostname) {
die ("Fatal: passwd-file is exist! Rename or remove it before runing this script!");
}
open (PWFILE, ">>$file");
while(($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) = getpwent()) {
        if ( $uid >= "1000" ){
          unless ( $name =~ m/nobody/isx ){
                ($tmp, $home, $domain) = split( /\//,$dir);
                if ( $domain =~ m/\w*.\..*/s ) {
                        $string="$name\@$domain:$passwd:$uid:$gid\:\:$dir\:\:maildir\:$dir";
                }
                else {
                        $string="$name\@$hostname:$passwd:$uid:$gid\:\:$dir\:\:maildir\:$dir";
                }
                print PWFILE "$string\n";
                next;
      }
        } else {
                next;
      }
}
close PWFILE;

Praporshic

  • Гість
Re: Dovecot.passwd
« Відповідей #1 : 2007-01-11 22:49:20 »
Невеликий додаток до комплекту: конвертер mailbox -> maildir
#! /usr/bin/perl
#####################################################################################################
# Mailbox to maildir converting script
# Based on Russell Nelson`s convert-and-create script
# Message-ID is used for mailfile name
#####################################################################################################
while(($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) =
      getpwent()) {
    if ($uid >= "1000") {
     if (!-e $dir) {
      print "warning: ${name}'s home dir, $dir, doesn't exist (passwd: $passwd), skipping.\n";
      next;
     }
     $st_uid = (stat($dir))[4];;
     if ($uid != $st_uid) {
       print "warning: $name is $uid, but $dir is owned by $st_uid, skipping.\n";
       next;
     }
    print "$name\n";
     $spoolname = "$dir/.maildir";
     -d $spoolname || mkdir $spoolname,0700 || print "fatal: mailbox doesn't exist and can't be created.\n";
     chown ($uid,$gid,$spoolname);
     chdir($spoolname) || print ("fatal: unable to chdir to $spoolname.\n");
     -d "tmp" || mkdir("tmp",0700) || print "fatal: unable to make tmp/ subdir\n";
     -d "new" || mkdir("new",0700) || print "fatal: unable to make new/ subdir\n";
     -d "cur" || mkdir("cur",0700) || print "fatal: unable to make cur/ subdir\n";
     chown ($uid,$gid,"tmp","new","cur");

    open(SPOOL, "</var/spool/mail/$name") || next;
    $i = time;
    while(<SPOOL>) {
         if (/^From /) {
              $fn = sprintf("new/%d.tmp", $i);
              open(OUT, ">$fn") || die("fatal: unable to create new message");;
              chown ($uid,$gid,$fn);
              $i++;
              next;
         }
       s/^>From /From /;
      print OUT || die("fatal: unable to write to new message");

    }
    close(SPOOL);
    close(OUT);
    }
    chdir ($spoolname."/new/");
    while (<*.tmp>) {
        @files = <*.tmp>;
      print "@files[0]";
      open (MSG, "<@files[0]");
      while(<MSG>) {
      if (/^Message-Id:.\W(\d+\..*\@[\w+\.]*\w+)\W/){
          $msgid = $1;
          print "$msgid";
          }
          }
      close(MSG);
          rename $spoolname."/new/".@files[0], $spoolname."/new/".$msgid;
      }
}
endpwent();
Код не дуже правильний, але працює без зайвих проблем.