License

Copyright (C) 2008-2021 Oliver Bohlen.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.

A copy of the license is included in the section entitled "GNU Free Documentation License".

Introduction

This documentation comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law.

Howto: IMAP/POP3-Server for Gentoo Linux

Here is my IMAP/POP3-Server configuration. I'm using this in combination with postfix and the webmailer Horde. It is also possible to use a mailclient like thunderbird.

If you want to use this solution you need the following howto(s) finished:

Required software

The required software has to be installed with the following command(s):
emerge net-mail/cyrus-imapd

Changes in /etc/cron.daily/cyrus-purge.sh

File permissions:
Owner: root
Group: root
Permissions: -rwx------

Click here for a download of the complete file: /etc/cron.daily/cyrus-purge.sh

Changed on 07.07.09
Issued by olli
Beginning line 2

This is a daily cron job that deletes all messages in of the admin-User older then 30 days.

#!/bin/bash
ipurge -d30 -X -f user/admin >/dev/null
ipurge -d30 -X -f user/admin/% >/dev/null
ipurge -d90 -X -f user/%/Spam >/dev/null
ipurge -d90 -X -f user/%/Junk >/dev/null
ipurge -d90 -X -f user/%/Papierkorb >/dev/null
ipurge -d90 -X -f user/%/Trash >/dev/null
ipurge -d3650 -X -f user/%/Sent >/dev/null
ipurge -d365 -X -f user/marco/Terminmails >/dev/null
ipurge -d90 -X -f user/user1/Server >/dev/null
find /var/spool/imap/*/user/*/Sent* -type f -name '*.' -size +1M -mtime +365 -delete
cyr_expire -D 60 -X 60
/usr/sbin/reconstruct -fGRr % >/dev/null

Changes in /etc/profile.d/cyrus.sh

File permissions:
Owner: root
Group: root
Permissions: -rwxr-xr-x

Click here for a download of the complete file: /etc/profile.d/cyrus.sh

Changed on 17.08.10
Issued by olli
Beginning line 1

Add the bin-path of the cyrus-progs to the default path-variable

PATH="$PATH:/usr/lib/cyrus"

Changes in /usr/local/sbin/cyr-create-mbox

File permissions:
Owner: root
Group: root
Permissions: -rwx------

Click here for a download of the complete file: /usr/local/sbin/cyr-create-mbox

Changed on 10.06.09
Issued by olli
Beginning line 2

This is a script for creating cyrus mailboxes.

#!/usr/bin/perl


unless ($ARGV[0]=~/^[a-zA-Z0-9\/\.]+$/) { error("Mit den übergebenen Daten stimmt was nicht!\n$ARGV[0] ist kein gültiger MBOX-Name\nAufruf: $0 MBOXNAME MBOXGRÖßE") }
#unless (($ARGV[1]=~/\d{2}/) && ($ARGV[1]<=9999)) { error("Mit den übergebenen Daten stimmt was nicht!\n$ARGV[1] ist kein erlaubter Wert (Minimal 10 / Maximal 9999)\nAufruf: $0 MBOXNAME MBOXGRÖßE") }


chomp($ARGV[0], $ARGV[1]);
$mbox=$ARGV[0];
$spaceusage=$ARGV[1];
$mboxspace=$ARGV[1]*1024;
$space=$ARGV[1];

use Cyrus::IMAP::Admin;
$cyrus = Cyrus::IMAP::Admin->new("localhost");
$cyrpass=`gtc-crypt -a admin -p`;
chomp($cyrpass);
$cyrus->authenticate('login','imap','','admin','0','10000',$cyrpass);

unless ($cyrus->listmailbox($mbox)) { 
 $cyrus->createmailbox($mbox) || error("Konnte Mailbox $mbox nicht erstellen: $!");
}
$recover=2;

if ($mbox=~/^user\/[a-zA-Z0-9]+$/) {
 $cyrus->createmailbox("$mbox/Drafts") || error("Konnte Mailbox $mbox.Drafts nicht erstellen: $!") unless ($cyrus->listmailbox("$mbox/Drafts"));
 $cyrus->createmailbox("$mbox/Sent") || error("Konnte Mailbox $mbox.Sent nicht erstellen: $!") unless ($cyrus->listmailbox("$mbox/Sent"));
 $cyrus->createmailbox("$mbox/Trash") || error("Konnte Mailbox $mbox.Trash nicht erstellen: $!") unless ($cyrus->listmailbox("$mbox/Trash"));;
 $cyrus->createmailbox("$mbox/Spam") || error("Konnte Mailbox $mbox.Spam nicht erstellen: $!") unless ($cyrus->listmailbox("$mbox/Spam"));
}
else {
 $cyrus->setacl($mbox, anyone => none) || error("Kann die Rechte nicht setzen: $@"); 
}

if ($ARGV[1]) {
 $cyrus->setquota($mbox, STORAGE, $mboxspace) || error("Konnte Quota von $mbox nicht auf $spaceusage setzen: $@");
}


exit 0;

sub error {
 $message=shift;
 if ($recover>=2) { 
  $cyrus = Cyrus::IMAP::Admin->new("localhost") || warn "Recovery: Keine Verbindung zu $cyrhost: $@";
  $cyrus->authenticate('login','imap','','admin','0','10000',$cyrpass) || warn "Recovery: Keine Authentifizierung auf $cyrhost als $cyruser möglich: $@";
  $cyrus->setaclmailbox($mbox, $cyruser => "c") || warn "Recovery: Fehler beim setzen der lösch Rechte auf $mbox: $@";
  $cyrus->deletemailbox($mbox) || warn "Recovery: Konnte $mbox nicht wieder löschen: $@";
 }
 die "$message";
}

Changes in /usr/local/sbin/cyr-delete-mbox

File permissions:
Owner: root
Group: root
Permissions: -rwx------

Click here for a download of the complete file: /usr/local/sbin/cyr-delete-mbox

Changed on 10.06.09
Issued by olli
Beginning line 2

This is a script for deleting cyrus mailboxes.

#!/usr/bin/perl

# Nötige Informationen:
# - MBOXName arg0

unless ($ARGV[0]=~/^[a-zA-Z0-9\/\.]+$/) { error("Mit den übergebenen Daten stimmt was nicht!\n$ARGV[0] ist kein gültiger MBOX-Name\nAufruf: $0 MBOXNAME") }


# Newlines entfernen
chomp($ARGV[0]);
# mbox setzen 
$mbox=$ARGV[0];

## Jetzt gehts aber wirklich los ##

use Cyrus::IMAP::Admin;

# Connect to Cyrus
$cyrus = Cyrus::IMAP::Admin->new("localhost");

# Authentifizieren
$cyrpass=`gtc-crypt -a admin -p`;
chomp($cyrpass);
$cyrus->authenticate('login','imap','','admin','0','10000',$cyrpass);
$cyrpass="";

# Checken ob MBOX schon existiert existiert
unless ($cyrus->listmailbox($mbox)) { error("MBOX $mbox gibt es nicht") }

# Lösch-Rechte setzen
$cyrus->setaclmailbox($mbox, 'admin' => "c") || error("Konnte Mailboxrechte von $mbox nicht auf c ändern: $!");

# Mailbox löschen
$cyrus->deletemailbox($mbox) || error("Konnte Mailbox $mbox nicht löschen: $!");


exit 0;

sub error {
 $message=shift;
 die "$message";
}

Changes in /usr/local/sbin/cyr-resize-mailbox.pl

File permissions:
Owner: root
Group: root
Permissions: -rwx------

Click here for a download of the complete file: /usr/local/sbin/cyr-resize-mailbox.pl

Changed on 10.06.09
Issued by olli
Beginning line 2

This is a script for changing quota on cyrus mailboxes.

#!/usr/bin/perl


##### Los gehts :-) #####

## Etwas Vorgeplänkel noch ##

# Übergabeparameter checken
unless ($ARGV[0]=~/^[a-zA-Z0-9\-\/\.]+$/) { error("Mit den übergebenen Daten stimmt was nicht!\n$ARGV[0] ist kein gültiger MBOX-Name\nAufruf: $0 MBOXNAME MBOXGRÖßE") }
unless (($ARGV[1]=~/\d{2}/) && ($ARGV[1]<=99999)) { error("Mit den übergebenen Daten stimmt was nicht!\n$ARGV[1] ist kein erlaubter Wert (Minimal 10 / Maximal 99999)\nAufruf: $0 MBOXNAME MBOXGRÖßE") }


# Newlines entfernen
chomp($ARGV[0], $ARGV[1]);
# mbox setzen
$mbox=$ARGV[0];
# Benötigter Speicherplatz der neuen MBOX
$spaceusage=$ARGV[1];
# MBox-Größe für DB-Eintrag
$space=$ARGV[1];
# Größe des mboxspaces für cyrus berechnen
$mboxspace=$ARGV[1]*1024;

## Jetzt gehts aber wirklich los ##
use Cyrus::IMAP::Admin;

# Connect to Cyrus
$cyrus = Cyrus::IMAP::Admin->new("localhost");
#
# # Authentifizieren
$cyrpass=`gtc-crypt -a admin -p`;
chomp($cyrpass);
$cyrus->authenticate('login','imap','','admin','0','10000',$cyrpass);
$cyrpass="";
#

# Checken ob MBOX existiert
unless ($cyrus->listmailbox($mbox)) { error("MBOX $mbox gibt es nicht") }

# Prüfen ob Quote nicht zu klein für die Datenmenge in der Mailbox ist
# Quota der MAilbox holen
%quota = $cyrus->listquota("$mbox");
# Schlüssel entsprechend durchgehen
foreach (keys(%quota)) {
 if ($mboxspace < $quota{$_}[0]) {
  error("Neues Quota ($mboxspace) zu klein für Mailbox ($quota{$_}[0])");
 }
 # Benötigten Speicherplatz ermitteln
 $spaceusage=-$quota{$_}[1];
}

# Quota auf die Mailbox setzen
$cyrus->setquota($mbox, STORAGE, $mboxspace) || error("Konnte Quote von $mbox nicht auf $mboxspace setzen: $@");



exit 0;

sub error {
 $message=shift;
 die "$message";
}

Changes in /usr/local/sbin/cyr-set-acl

File permissions:
Owner: root
Group: root
Permissions: -rwx------

Click here for a download of the complete file: /usr/local/sbin/cyr-set-acl

Changed on 10.06.09
Issued by olli
Beginning line 2

This is a script for changing rights on cyrus mailboxes.

#!/usr/bin/perl

unless ($ARGV[0]=~/^[a-zA-Z0-9\.\/\- \&]+$/) { error("Mit den übergebenen Daten stimmt was nicht!\n$ARGV[0] ist kein gültiger MBOX-Name\nAufruf: $0 MBOXNAME USER RECHT") }
unless ($ARGV[1]=~/^[a-zA-Z0-9\.]+$/) { error("Mit den übergebenen Daten stimmt was nicht!\n$ARGV[1] ist kein gültiger User-Name\nAufruf: $0 MBOXNAME USER RECHT") }
#unless ($ARGV[2]=~/^[lrswipkxtecdanoa]+$/) { error("Mit den übergebenen Daten stimmt was nicht!\n$ARGV[2] ist kein gültiges Recht\nAufruf: $0 MBOXNAME USER RECHT") }




# Newlines entfernen
chomp($ARGV[0], $ARGV[1], $ARGV[2]);
# mbox setzen 
$mbox=$ARGV[0];
# User
$user=$ARGV[1];
# Recht
$right=$ARGV[2];

## Jetzt gehts aber wirklich los ##

use Cyrus::IMAP::Admin;

# Connect to Cyrus
$cyrus = Cyrus::IMAP::Admin->new("localhost");

# Authentifizieren
$cyrpass=`gtc-crypt -a admin -p`;
chomp($cyrpass);
$cyrus->authenticate('login','imap','','admin','0','10000',$cyrpass);
$cyrpass="";



# Checken ob MBOX  existiert
unless ($cyrus->listmailbox($mbox)) { error("MBOX gibt es nicht") }

# Rechte setzen
$cyrus->setacl($mbox, $user => $right) || error("Kann die Rechte nicht setzen: $@");


exit 0;

sub error {
 $message=shift;
 # Je nach Fehlerzeitpunkt (recover-wert) Recovery starten, wenn nötig.
 # Lockdatei löschen
 # Mit Fehlermeldung beenden
 die "$message";
}

Changes in /usr/local/sbin/cyr-set-sieve.sh

File permissions:
Owner: root
Group: root
Permissions: -rwx------

Click here for a download of the complete file: /usr/local/sbin/cyr-set-sieve.sh

Changed on 10.06.09
Issued by olli
Beginning line 2

This is a script for setting the default sieve-filter-script for a user. The script which is set is /usr/local/etc/sieve.script.default which you have to create.

#!/bin/bash
/usr/lib/cyrus/sievec /usr/local/etc/sieve.script.default /tmp/default.bc
chmod 600 /tmp/default.bc
chown cyrus:mail /tmp/default.bc
INITIAL=`echo $1 | cut -c1`
NAME=$1
cd /var/imap/sieve/$INITIAL/
mkdir -p $NAME
chown cyrus:mail $NAME
cd /var/imap/sieve/$INITIAL/$NAME/
cp /usr/local/etc/sieve.script.default default.script
mv /tmp/default.bc .
ln -sf default.bc defaultbc

Changes in /usr/local/sbin/cyr-show-dirs

File permissions:
Owner: root
Group: root
Permissions: -rwx------

Click here for a download of the complete file: /usr/local/sbin/cyr-show-dirs

Changed on 10.06.09
Issued by olli
Beginning line 2

This is a script for listing all your cyrus directories.

#!/usr/bin/perl

use Cyrus::IMAP::Admin;

# Connect to Cyrus
$cyrus = Cyrus::IMAP::Admin->new("localhost");

# Authentifizieren
$cyrpass=`gtc-crypt -a admin -p`;
chomp($cyrpass);
$cyrus->authenticate('login','imap','','admin','0','10000',$cyrpass);
$cyrpass="";

# Header ausgeben
print "Verzeichisse bzw. Mailboxes:\n\n";
# Mailbox- Infos holen
@acl=$cyrus->listmailbox('*');
# Ausgabe durchgehen
foreach (@acl) {
 $i++;
 if ($acl[$i][0]) {
  # Variable zum angenehmeren Lesen setzen
  $mbox=$acl[$i][0];
  # ACLs vom Server holen
  %acls = $cyrus->listacl("$mbox");
  # Alte ACL-Variable loeschen
  $acl="";
  # ACL-Variable zusammenbauen
  foreach (keys(%acls)) { $acl="$acl $_ -> $acls{$_}   " }
  # Quotas vom Server holen
  %quota = $cyrus->listquota("$mbox");
  # Ausgabe durchgehen
  foreach (keys(%quota)) {
   # Gibt es fuer die Box ein Quota?
   if ($quota{$_}[1]) {
    # in MB umrechnen
    $benutzt=$quota{$_}[0]/1024;
    $gesamt=$quota{$_}[1]/1024;
    # Werte ohne , runden.
    $benutzt=sprintf("%.0f", $benutzt);
    $gesamt=sprintf("%.0f", $gesamt);
    # % ausrechnen und %-Zeichen dran packen
    $prozent=(100/$gesamt)*$benutzt . "%";
    # Prozent runden
    $prozent=sprintf("%.0f", $prozent);
   }
  }
  # Dir mit Acl ausgeben
  print "$mbox\n  Quota: Benutzt: $benutzt\tGesamt: $gesamt\tProzent: $prozent\%\n  Rechte: $acl\n";
 }
 # Kommt mix mehr dann beenden
 else { last }
}

# LockDatei wieder loeschen
unlink $lockfile_file;

Changes in /usr/local/sbin/cyr-show-mailboxes

File permissions:
Owner: root
Group: root
Permissions: -rwx------

Click here for a download of the complete file: /usr/local/sbin/cyr-show-mailboxes

Changed on 10.06.09
Issued by olli
Beginning line 2

This is a script for listing all your cyrus mailboxes.

#!/usr/bin/perl
use Cyrus::IMAP::Admin;

# Connect to Cyrus
$cyrus = Cyrus::IMAP::Admin->new("localhost");

# Authentifizieren
$cyrpass=`gtc-crypt -a admin -p`;
chomp($cyrpass);
$cyrus->authenticate('login','imap','','admin','0','10000',$cyrpass);
$cyrpass="";

# Header ausgeben
print "Mailboxes und Quotas (in MB)\n";
print "Mailbox                     Used        Free        Percent  Rights\n";
print "-------------------------------------------------------------------------------------------------------->\n";
# Mailbox- Infos holen
@mboxes=$cyrus->listmailbox('*');
# Ausgabe durchgehen
foreach (@mboxes) {
 $i++;
 if ($mboxes[$i][0]) {
  # Variable zum angenehmeren Lesen setzen
  $mbox=$mboxes[$i][0];
  # Quotas vom Server holen
  %quota = $cyrus->listquota("$mbox");
  # Ausgabe durchgehen
  foreach (keys(%quota)) {
   #print "$mbox - $quota{$_}[1]\n";
   # Gibt es fuer die Box ein Quota?
   if ($quota{$_}[1]) {
    # ACLs vom Server holen
    %acls = $cyrus->listacl("$mbox");
    # Alte ACL-Variable löschen
    $acl="";
    # ACL-Variable zusammenbauen
    foreach (keys(%acls)) { $acl="$acl $_ -> $acls{$_}   " }
    # in MB umrechnen
    $benutzt=$quota{$_}[0]/1024;
    $gesamt=$quota{$_}[1]/1024;
    # Werte ohne , runden.
    $benutzt=sprintf("%.0f", $benutzt);
    $gesamt=sprintf("%.0f", $gesamt);
    # % ausrechnen und %-Zeichen dran packen
    $prozent=(100/$gesamt)*$benutzt . "%";
    # Prozent runden
    $prozent=sprintf("%.0f", $prozent);
    # Kram formatiert ausgeben
    format STDOUT =
@<<<<<<<<<<<<<<<<<<<<<<<    @<<<<<<<<<  @<<<<<<<<<  @<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$mbox, $benutzt, $gesamt, $prozent, $acl
.
    write;
   }
  }
 }
 # Kommt mix mehr dann beenden
 elsif ($i>=1000) { last }
}

# Quota der mailbox holen
@quota = $cyrus->listquota('*');
foreach $key (keys %quota) {
}

# LockDatei wieder loeschen
unlink $lockfile_file;

Setting up services

For starting the new service after system reboot you should add it to a runlevel with the following command(s):

rc-update add cyrus 

Please send a feedback to: doc<at>gabosh.net

Howto listing
File Index

Here you can find the official Gentoo Linux Forums where you can find a lot of answers.

Here a link to the official Gentoo Linux Homepage.

Edit Howto

About / Impressum

Click here for About / Impressum

Wishlist

If you want to support my work you can find my Amazon whishlist here