Virtuelle E-Mail User erstellen

Aus

Mailbenutzer erstellen

Wenn ihr neue E-Mail Benutzer erstellen wollt, dann geht das am Schnellsten via dem DSM. Der Nachteil dieser Lösung ist es, dass man dann für jeden E-Mail Benutzer einen lokalen Systemaccount einrichten muss. Dafür gibt es virtuelle Benutzer, welche es ermöglichen alle Mailprozesse unter einem lokalen User fahren zu lassen. Das hat den Vorteil, dass man für neue E-Mail Nutzer nur noch einen virtuellen Benutzer für die Mailserver anlegen muss. Das System (also /etc/passwd) kennt diese Mailbenutzer jedoch nicht. Darum virtuell ;) Mehr dazu wie man virtuelle Nutzer für Postfix und Dovecot erstellen kann.

Da man virtuelle Benutzer nicht über den DSM anlegen kann, habe ich folgendes kleine Script geschrieben, welches dem admin beim Einrichten der User hilft. Ihr könnt den Code in meinem svn finden oder weiter unten direkt in diesem Beitrag

config

Die Config Sektion des Codes muss man sich nach seinen Gegebenheiten anpassen. Das Script schreibt in Systemfiles und darum sind alle Pfade von Files die geschrieben werden im Auslieferungszustand auf /tmp gesetzt. Damit kann man das Script ausprobieren und schauen ob die generierten/angepassten Files in /tmp soweit korrekt sind.
Im folgenden eine kurze Beschreibung der Konfig (diese bezieht sich auf die devel-Version des Scripts!)

global

mail

Legt das Root Verzeichnis der virtuellen User fest

mail=/tmp/dovecot
user

Gibt den Usernamen des lokalen Benutzers an unter dessen Kennung die virtuellen Benutzer "laufen". Wird für den chown auf dem neuangelegten Mailbox Verzeichnis benötigt

user=vmail
group

Siehe user

group=vmail

Dovecot

Config für Dovecot

dovecot_pw_file

Pfad zum Dovecot Passwort File, welches die virtuellen User und deren Passworte enthält (gebt hier NIEMALS /etc/passwd an!!!)

dovecot_pw_file=/tmp/dovecot/passwd
dovecotpw

Pfad zum dovecotpw Binary. Dieses wird jedoch nicht in der Firmware (Mailstation) mitgeliefert. Man kann es sich jedoch aus den Quellen selbe kompillieren oder dieses Script mit dem Parameter -i oder --install aufrufen. Dann wird dovecotpw aus dem ipkg dovecot installiert.

dovecotpw=/usr/syno/mailstation/libexec/dovecot/dovecotpw
scheme

Legt das zu verwendende Schema bei der Erstellung der Passworte für die virtuellen User fest.

scheme=SSHA

Postfix

Konfiguration der Postfix-Seite

postfix

Pfad zum Postfix Binary. Wird zum Neuladen der Konfig nach Erstellen der Nutzer benötigt

postfix=/usr/syno/mailstation/sbin/postfix
postfix_postmap

Pfad zum postmap Kommando. Dieses wird benötigt um aus den virtuellen Userfiles des Postfix die benötigten db Files zu erstellen

postfix_postmap=/usr/syno/mailstation/sbin/postmap
postfix_vmailbox

Legt den Pfad für das Postfix vmailbox File fest

postfix_vmailbox=/tmp/dovecot/vmailbox
postfix_virtual

Pfad zum Postfix File mit den Alias Namen der virtuellen Benutzer

postfix_virtual=/tmp/dovecot/virtual
postfix_main

Der Pfad zum main.cf File des Postfix Servers. Darin wird nichts geschrieben! Das Script nutzt das File um zu prüfen, ob die im Script angegebenen Files auch in der Postfix Config korrekt gesetzt sind

postfix_main=/usr/syno/mailstation/etc/main.cf

Script benutzen

Das Script ist ein "einfaches" Shellscript für die Kommandokonsole. Es sollte von root ausgeführt werden. Sonst bestehen keine Schreibrechte auf die benötigten Konfigfiles.

Parameter

Es gibt einige Parameter, die ich hier kurz erläutern möchte

-i|--install

Wenn dovecotpw nicht vorhanden ist, kann man mit diesem Parameter das Script anweisen dovecotpw von ipkg zu laden. Dabei wird natürlich ein funktionierendes ipkg und eine Internetverbindung benötigt

-h|--help

Zeigt die "Hilfe" des Scripts an

user

Legt den Usernamen des neu zu installierenden E-Mail Nutzers fest. Es handelt sich hierbei nur um den Local Part der Adresse, also alles vor dem @!

domain

Legt den Domainnamen des Nutzers fest. Es handelt sich hierbei nur um den Domain Part der Adresse also alles hinter dem @!

password

Dürfte selbsterklärend sein, wofür dieser Parameter ist ;-)

Aufrufen

Der Aufruf des Scriptes ist ziemlich einfach

createMaildir foo bar.tld secret

erstellt im mail Verzeichnis aus der Konfiguration - falls nicht vorhanden - das Verzeichnis bar.tld für die Domain bar.tld an. Darin wird das Verzeichnis foo für den User foo erstellt. Dortdrin wird dann das .Maildir Verzeichnis angelegt, wo die E-Mails drin landen. Die Mailbox also. secret wird als Passwort für den User foo@bar.tld in dovecot_pw_file verwendet.
Bei obigem Aufruf wird also im Mailroot die folgende Struktur angelegt

/volume1/homes/vmail
 + bar.tld
 ++ foo
 +++ .Maildir

Quellcode

#!/bin/sh

###########################################################################
#                                                                         #
#                           createMaildir Script                          #
#                                                                         #
#                   <author>      tobster@brain-force.ch                  #
#                   <version>     Version 0.1b                            #
#                   <date>        4th April 2010                          #
#                   <license>     GNU GPL V 3                             #
#                   <url>         http://svn.brain-force.ch/linuxstuff    #
#                                                                         #
# This script only works IF local mail derlivery is performed via deliver #
# Check your system whether you have deliver on it. Otherwise you have to #
# compile it from Dovecot source which can be found here:                 #
#                                                                         #
#               http://www.dovecot.org/download.html                      #
#                                                                         #
# Furthermore this script requieres dovecotpw to be installed. If missing #
# the code can be obtained from the source above. Or you can call the     #
# script with -i or --install parameter to get dovecotpw from ipkg        #
#                                                                         #
# This code is under GPL license. Feel free to modify as you may wish     #
#                                                                         #
# See config section for more details                                     #
#                                                                         #
###########################################################################


#### BEGIN OF "CONFIG" SECTION ####
## change the following variables to fit your enviroment
## if you don't have dovecotpw on your system just set the dovecotpw variable to a valid value on your box
## and call the script with -i or --install parameter. The script will install dovecotpw to the location specified in $dovecotpw
##
## root directory for mailboxes.
mail=/tmp/dovecot
##
# path to the dovecot password file. This implies that you have deliver installed or compiled from source
dovecot_pw_file=/tmp/dovecot/passwd
##
# Variables for interaction with postfix server
postfix=/usr/syno/mailstation/sbin/postfix
postfix_postmap=/usr/syno/mailstation/sbin/postmap
postfix_vmailbox=/tmp/dovecot/vmailbox
postfix_virtual=/tmp/dovecot/virtual
postfix_main=/usr/syno/mailstation/etc/main.cf
##
## user and group for chown on mailbox directory
user=vmail
group=vmail
##
## path to dovecotpw binary (see comment on top!) and the desired password scheme to use
## you can check the schemes your system supports by dovecotpw -l
dovecotpw=/usr/syno/mailstation/libexec/dovecot/dovecotpw
scheme=SSHA
##
## IMPORTANT: The installed dovecotpw file is made for Dovecot V 1.2
## So if you use Dovecot < 1.2 then you should NOT USE one of the
## new password schemes introduced in 1.2. like ex SSHA256.
## SSHA and MD5 work fine!
#### END OF "CONFIG" SECTION ####

if test "$*" = '-i' || test "$*" = '--install'; then
 echo "create tmp dir and cd into"
 mkdir /tmp/dovecot
 cd /tmp/dovecot
 echo "download dovecot from ipkg"
 ipkg download dovecot > /dev/null 2>&1
 f=$(find ./ -type f | grep ipk)
 echo "download done. unpacking dovecot files"
 mv ./$f ./dovecot.tar.gz
 tar -xzf ./dovecot.tar.gz
 tar -xzf ./data.tar.gz
 echo "copy file to destination and clean up tmp files"
 cp -f ./opt/sbin/dovecotpw $dovecotpw
 rm -R /tmp/dovecot
 echo
 echo "installation complete. You should see to output of available schemes from dovecotpw now:"
 $dovecotpw -l
 echo
 echo " ###########################################################################"
 echo " ##                                                                       ##"
 echo " ## IMPORTANT: The installed dovecotpw file is made for Dovecot V 1.2     ##"
 echo " ## So if you use Dovecot < 1.2 then you should NOT USE one of the        ##"
 echo " ## new password schemes introduced in 1.2. like ex SSHA256.              ##"
 echo " ## SSHA and MD5 work fine!                                               ##"
 echo " ##                                                                       ##"
 echo " ###########################################################################"
 echo
 exit
elif test ! -e $mail || test ! -d $mail; then
 echo "ERROR: $mail could not be found"
 exit
elif test ! -e $dovecot_pw_file; then
 echo "ERROR: $dovecot_pw_file could not be found. Check config!"
 exit
elif test ! -e $dovecotpw || test ! -x $dovecotpw; then
 echo "ERROR: $dovecotpw not found or not executable."
 echo "       Ensure that dovecotpw is set correctly"
 exit
elif test ! -e $postfix_postmap || test ! -x $postfix_postmap; then
 echo "ERROR: Check that $postfix_postmap exists and is executable."
 exit
elif test ! -e $postfix_virtual || test ! -e $postfix_vmailbox; then
 echo "ERROR: Ensure that $postfix_virtual AND $postfix_vmailbox exist"
 exit
elif test ! -e $postfix || test ! -x $postfix; then
 echo "ERROR: $postfix must be present and executable"
 exit
elif test -z $(cat /etc/passwd | grep "$user:"); then
 echo "ERROR: $user not found in /etc/passwd"
 exit
elif test -z $(cat /etc/group | grep "$group:"); then
 echo "ERROR: $group not found in /etc/groups"
 exit
elif test -e $dovecotpw && test -z "$($dovecotpw -l | grep $scheme)"; then
 echo "ERROR: your system does not support $scheme encryption"
 echo "       the following schemes are supported on your platform:"
 echo $($dovecotpw -l)
 exit
elif test -z $(cat $postfix_main | grep ^virtual_mailbox_maps | grep $postfix_vmailbox); then
 echo "ERROR: $postfix_vmailbox not found in \"virtual_mailbox_maps\" within $postfix_main"
 exit
elif test -z $(cat $postfix_main | grep ^virtual_alias_maps | grep $postfix_virtual); then
 echo "ERROR: $postfix_virtual not found in \"virtual_alias_maps_\" within $postfix_main"
 exit
elif test -z $(cat $postfix_main | grep ^virtual_mailbox_base | grep $mail); then
 echo "$mail not found in \"virtual_mailbox_root\" within $postfix_main"
 exit
fi
uname=''
domain=''
pw=''
if test $# -eq 3; then
 uname=$(echo $1 | grep ^[a-zA-Z0-9._\-]*$)
 domain=$(echo $2 | grep ^[a-zA-Z0-9._\-]*$)
 pw=$3
 if test "$uname" = '' || test "$domain" = ''; then
  echo "Only chars in [a-zA-Z0-9._\-] are allowed for username or domain"
  exit
 elif test -e "$mail/$domain/$uname"; then
  echo "$mail/$domain/$uname exists already. Nothing to be done. Exit"
  exit
 else
  echo "create $mail/$domain/$uname/.Maildir and chown to $user:$group"
  mkdir -p "$mail/$domain/$uname/.Maildir"
  chown -R $user:$group "$mail/$domain"
  echo $uname@$domain:$($dovecotpw -p "$pw" -s $scheme) >> $dovecot_pw_file
  echo $uname@$domain $domain/$uname/.Maildir >> $postfix_vmailbox
  echo $uname@$domain $uname@$domain >> $postfix_virtual
  $postfix_postmap $postfix_vmailbox
  $postfix_postmap $postfix_virtual
  echo "Account successfully created. See details:"
  echo "Username: $uname@$domain"
  echo "Files created"
  ls -al "$mail/$domain"
  ls -al "$mail/$domain/$uname"
  echo "Line from $dovecot_pw_file"
  cat $dovecot_pw_file | grep $uname@$domain
  echo "Line from $postfix_vmailbox"
  cat $postfix_vmailbox | grep "$domain/$uname/.Maildir"
  echo "Line from $postfix_virtual"
  cat $postfix_virtual | grep $uname@$domain
  echo
  echo "Check $postfix_main"
  if test -z $(cat $postfix_main | grep ^virtual_mailbox_domains | grep $domain); then
   f=$(cat $postfix_main | grep ^virtual_mailbox_domains)
   echo $f $domain
  fi
  echo "Reload postfix config files" $($postfix reload)
 fi
elif test "$*" = '-h' || test "$*" = '--help'; then
 echo
 echo " ###########################################################################"
 echo " #                                                                         #"
 echo " # Usage:                                                                  #"
 echo " # createMaildir  [-i|--install] [-h|--help] <user> <domain> <password>    #"
 echo " #                                                                         #"
 echo " # -i | --install Installs the necessary dovecotpw from ipkg.              #"
 echo " #                Needs ipkg to be installed!                              #"
 echo " # -h | --help    Show this message                                        #"
 echo " # <user>         Username of the account to be created.                   #"
 echo " #                IMPORTANT: Username only, no domain part                 #"
 echo " # <domain>       Domainname for the user account.                         #"
 echo " #                Folders are created like this /path/<domain>/<user>      #"
 echo " # <password>     Password for the account                                 #"
 echo " #                                                                         #"
 echo " #                <user>, <domain> and <password> are ALL MANDATORY        #"
 echo " #                if a useraccount is to be created                        #"
 echo " #                Example usage: createMaildir myuser mydomain mysecret    #"
 echo " #                                                                         #"
 echo " ###########################################################################"
 exit
else
 echo
 echo " ###########################################################################"
 echo " #                                                                         #"
 echo " #                           createMaildir Script                          #"
 echo " #                                                                         #"
 echo " #                   <author>      tobster@brain-force.ch                  #"
 echo " #                   <version>     Version 0.1b                            #"
 echo " #                   <date>        4th April 2010                          #"
 echo " #                   <license>     GNU GPL V 3                             #"
 echo " #                   <url>         http://svn.brain-force.ch/linuxstuff    #"
 echo " #                                                                         #"
 echo " # This script helps admins to create new users for their mailserver.      #"
 echo " # This script only works IF local mail derlivery is performed via deliver #"
 echo " # Check your system whether you have deliver on it. Otherwise you have to #"
 echo " # compile it from Dovecot source which can be found here:                 #"
 echo " #                                                                         #"
 echo " #               http://www.dovecot.org/download.html                      #"
 echo " #                                                                         #"
 echo " # Furthermore this script requieres dovecotpw to be installed. If missing #"
 echo " # the code can be obtained from the source above. Or you can call the     #"
 echo " # script with -i or --install parameter to get dovecotpw from ipkg        #"
 echo " #                                                                         #"
 echo " # This code is under GPL license. Feel free to modify as you may wish     #"
 echo " #                                                                         #"
 echo " ###########################################################################"
 echo
fi
exit