HowTo - mailserver med virtuella domäner och användare

Här kan du dela med dig av dina bästa tips och knep.
Kategoriregler
Här ligger de utförliga instruktionerna vi kan tänkas behöva. Leta här om du tex behöver installera nåt program eller sätta upp någon funktion. Starta inte trådar utan att ha ett svar.
Användarvisningsbild
northface
Inlägg: 501
Blev medlem: 15 dec 2007, 02:20
OS: Ubuntu
Utgåva: 22.04 Jammy Jellyfish LTS

HowTo - mailserver med virtuella domäner och användare

Inlägg av northface »

Nedanstående GRUNDINSTALLATION kan byggas på med TLS, SSL, ANTIVIRUS, ANTISPAM och WEBMAIL enligt http://www.ubuntu-se.org/phpBB3/viewtop ... 54&t=33709 .

Denna GRUNDINSTALLATION kräver inte MySQL!

Installera

Kod: Markera allt

sudo apt-get install postfix dovecot-common dovecot-imapd dovecot-pop3d whois
Skapa en virtuell mailboxgrupp

Kod: Markera allt

sudo groupadd -g 5000 vmail
Skapa en virtuell mailboxägare

Kod: Markera allt

sudo useradd -m -u 5000 -g 5000 -s /bin/bash vmail
Skapa en domänfil och kalla den vhosts

Kod: Markera allt

sudo touch /etc/postfix/vhosts
Öppna

Kod: Markera allt

sudo nano /etc/postfix/vhosts
Klistra in enligt nedan och spara

DinDomän1.se
DinDomän2.se



Konfigurera Postfix

Skapa en ny main.cf till Postfix och spara orginalet med ett nytt namn

Kod: Markera allt

sudo mv /etc/postfix/main.cf /etc/postfix/main.cf.orig
Skapa en ny main.cf

Kod: Markera allt

sudo touch /etc/postfix/main.cf
Öppna, klistra in nedan och spara

Kod: Markera allt

sudo nano /etc/postfix/main.cf
smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu/GNU)
biff = no

# appending .domain is the MUA's job.
append_dot_mydomain = no

# Uncomment the next line to generate "delayed mail" warnings
#delay_warning_time = 4h

myhostname = localhost
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = $myhostname
mynetworks = 127.0.0.0/8, <ditt egna interna nätverk>
mailbox_size_limit = 0
home_mailbox = Maildir/
relayhost = < din ISP:s relay server (typ smtp.bredband.net)>
virtual_mailbox_domains = /etc/postfix/vhosts
virtual_mailbox_base = /home/vmail
virtual_mailbox_maps = hash:/etc/postfix/vmaps
virtual_minimum_uid = 1000
virtual_uid_maps = static:5000
virtual_gid_maps = static:5000
recipient_delimiter = +
inet_interfaces = all


Konfigurera Dovecot

Skapa en ny dovecot.conf till Dovecot och spara orginalet med ett nytt namn

Kod: Markera allt

sudo mv /etc/dovecot/dovecot.conf /etc/dovecot/dovecot.conf.orig
Skapa en ny dovecot.conf

Kod: Markera allt

sudo touch /etc/dovecot/dovecot.conf
Öppna, klistra in nedan och spara

Kod: Markera allt

sudo nano /etc/dovecot/dovecot.conf
base_dir = /var/run/dovecot/
protocols = imap pop3
disable_plaintext_auth = no
shutdown_clients = yes
log_path = /var/log/dovecot
info_log_path = /var/log/dovecot.info
log_timestamp = "%Y-%m-%d %H:%M:%S "
ssl_disable = yes
login_dir = /var/run/dovecot/login
login_chroot = yes
login_user = dovecot
login_greeting = Dovecot ready.
mail_location = maildir:/home/vmail/%d/%n
mmap_disable = no
valid_chroot_dirs = /var/spool/vmail
protocol imap {
login_executable = /usr/lib/dovecot/imap-login
mail_executable = /usr/lib/dovecot/imap
}
protocol pop3 {
login_executable = /usr/lib/dovecot/pop3-login
mail_executable = /usr/lib/dovecot/pop3
pop3_uidl_format = %08Xu%08Xv
}
auth_executable = /usr/lib/dovecot/dovecot-auth
auth_verbose = yes
auth default {
mechanisms = plain digest-md5
passdb passwd-file {
args = /etc/dovecot/passwd
}
userdb passwd-file {
args = /etc/dovecot/users
}
user = root
}


Vi ska nu använda några script för att kunna lägga till virtuella användare och lösenord.

Script för att skapa användare

Skapa en fil med namnet adddovecotuser enligt

Kod: Markera allt

sudo touch /usr/sbin/adddovecotuser
Öppna

Kod: Markera allt

sudo nano /usr/sbin/adddovecotuser
Klistra in nedanstående

echo "$1" > /tmp/user
user=`cat /tmp/user | cut -f1 -d "@"`
domain=`cat /tmp/user | cut -f2 -d "@"`
echo "$user@$domain::5000:5000::/home/vmail/$domain/:/bin/false::" >> /etc/dovecot/users
/usr/bin/maildirmake.dovecot /home/vmail/$domain/$user 5000:5000
echo $1 $domain/$user/ >> /etc/postfix/vmaps
postmap /etc/postfix/vmaps
postfix reload

Gör filen exekverbar

Kod: Markera allt

sudo chmod +x /usr/sbin/adddovecotuser
Script för att skapa lösenord

Skapa en fil med namnet mkdovecotpasswd enligt

Kod: Markera allt

sudo touch /usr/sbin/mkdovecotpasswd
Öppna

Kod: Markera allt

sudo nano /usr/sbin/mkdovecotpasswd
Klistra in nedanstående

mkpasswd --hash=md5 $2 > /tmp/hash
echo "$1:`cat /tmp/hash`" >> /etc/dovecot/passwd

Gör filen exekverbar

Kod: Markera allt

sudo chmod +x /usr/sbin/mkdovecotpasswd
Skapa filen passwd enligt:

Kod: Markera allt

sudo touch /etc/dovecot/passwd
Ändra rättigheter

Kod: Markera allt

sudo chmod 640 /etc/dovecot/passwd
Vi skapar användare enligt modellen

Kod: Markera allt

sudo adddovecotuser nisse@DinDomän1.se
och sedan ett tillhörande lösenord enligt

Kod: Markera allt

sudo mkdovecotpasswd nisse@DinDomän1.se hemlis
Första gången en given domän kopplas ihop med en användare måste vi ändra ägarskap på domänen. (Bug?)

Kod: Markera allt

sudo chown vmail:vmail /home/vmail/DinDomän1.se
Starta om Postfix resp. Dovecot

Kod: Markera allt

sudo /etc/init.d/postfix restart
sudo /etc/init.d/dovecot restart
Starta alltid om Dovecot efter att en ny användare har lagts till!
radiopatrik
Inlägg: 21
Blev medlem: 07 feb 2009, 00:30
OS: Crunchbang

Re: HowTo - mailserver med virtuella domäner och användare

Inlägg av radiopatrik »

Här ska ni alla få lite krydda på steken.
Det kan ju vara smidigt om man kan lägga till användare samt virtuella domainer utan att behöva gå in på själva servern.

Jag har skapat ett sånt här script.
Scriptet fungerar aldelles utmärkt dock går det inte att ta bort domainer eller epost adresser. Jag jobbar på detta men så länge så funkar det här utmärkt.

Förutsättningar:
Du behöver, Apache och PHP5 för att kunna använda det här scriptet.

Steg 1

Gå till följande 2 filer och gör ändra så att "www-data" är ägare av dom samt att "www-data" kan skriva i filerna.

Kod: Markera allt

/etc/dovecot/users
/etc/dovecot/passwd
Gå till följande 2 filer och gör ändra så att "www-data" är ägare av dom samt att "www-data" kan skriva i filerna.

Kod: Markera allt

/etc/postfix/vmaps
/etc/postfix/vhosts
När det här är gjort så behöver vi såklart lägga in scriptet.

Lägg scriptet på din server (Obs!! Måste vara samma server som mail servern)

Och notera att det här är ett simpelt ihopslängt script som funkar bra men inget fancy.

Kod: Markera allt

<?php

print("<center><a href=?page=newemail><b>Lägg till en ny Epost adress</b></a></center><br />");
print("<center><a href=?page=newvhost><b>Lägg till en ny Virtual host (a.k.a ny epost domain att hosta)</b></a></center>");

$page = $_GET['page'];

if ($page == 'newemail') {

	print("<form method=\"post\" action=\"?page=newemail\">");
	print("<b>Email user:</b><br /> <input type=\"text\" name=\"emailuser\"><br />");
	print("<b>Domain:</b><br /> <input type=\"text\" name=\"domain\"><br />");
	print("<b>Lösenord:</b><br /> <input type=\"password\" name=\"passwd\"><br />");
	print("<input type=\"submit\" name=\"submit\" value=\"Lägg till Epost adress\">");
	print("</form>");
	
	$submit = $_POST['submit'];

	if (isset($submit)) {
		$users = '/etc/dovecot/users';
		$emailuser = $_POST['emailuser'];
		$domain = $_POST['domain'];
		$users_content = "".$emailuser."@".$domain."::5000:5000::/home/vmail/".$domain."/:/bin/false::\n";

		// Let's make sure the file exists and is writable first.
		if (is_writable($users)) {

			// In our example we're opening $users in append mode.
			// The file pointer is at the bottom of the file hence
			// that's where $users_content will go when we fwrite() it.
			if (!$handle = fopen($users, 'a')) {
				 echo "Cannot open file ($users)";
				 exit;
			}

			// Write $users_content to our opened file.
			if (fwrite($handle, $users_content) === FALSE) {
				echo "Cannot write to file ($users)";
				exit;
			}

			echo "Success, wrote ($users_content) to file ($users)";

			fclose($handle);

		} else {
			echo "The file $users is not writable";
		}
		
		$users = '/etc/dovecot/passwd';
		$emailuser = $_POST['emailuser'];
		$domain = $_POST['domain'];
		$passwd = $_POST['passwd'];
		$password = crypt($passwd);
		$users_content = "".$emailuser."@".$domain.":".$password."\n";

		// Let's make sure the file exists and is writable first.
		if (is_writable($users)) {

			// In our example we're opening $users in append mode.
			// The file pointer is at the bottom of the file hence
			// that's where $users_content will go when we fwrite() it.
			if (!$handle = fopen($users, 'a')) {
				 echo "Cannot open file ($users)";
				 exit;
			}

			// Write $users_content to our opened file.
			if (fwrite($handle, $users_content) === FALSE) {
				echo "Cannot write to file ($users)";
				exit;
			}

			echo "Success, wrote ($users_content) to file ($users)";

			fclose($handle);

		} else {
			echo "The file $users is not writable";
		}
		
		$vmaps = '/etc/postfix/vmaps';
		$emailuser = $_POST['emailuser'];
		$domain = $_POST['domain'];
		$vmaps_content = "".$emailuser."@".$domain." ".$domain."/".$emailuser."/\n";

		// Let's make sure the file exists and is writable first.
		if (is_writable($vmaps)) {

			// In our example we're opening $vmaps in append mode.
			// The file pointer is at the bottom of the file hence
			// that's where $vmaps_content will go when we fwrite() it.
			if (!$handle = fopen($vmaps, 'a')) {
				 echo "Cannot open file ($vmaps)";
				 exit;
			}

			// Write $vmaps_content to our opened file.
			if (fwrite($handle, $vmaps_content) === FALSE) {
				echo "Cannot write to file ($vmaps)";
				exit;
			}

			echo "Success, wrote ($vmaps_content) to file ($vmaps)";

			fclose($handle);

		} else {
			echo "The file $vmaps is not writable";
		}
	}
	
	$myFile = "/etc/dovecot/users";
	$fh = fopen($myFile, 'r');
	$theData = implode(', ', file($myFile));	 
	 
	$lines = file('/etc/dovecot/users');
	echo "<br><b>Users:</b><br><br>";
	// Loop through our array, show HTML source as HTML source; and line numbers too.

	foreach ($lines as $line_num => $line) {
		echo "<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
	}
	
	$myFile2 = "/etc/dovecot/passwd";
	$fh2 = fopen($myFile2, 'r');
	$theData2 = implode(', ', file($myFile2));	 
	 
	$lines2 = file('/etc/dovecot/passwd');
	echo "<br><b>Passwd:</b><br><br>";
	// Loop through our array, show HTML source as HTML source; and line numbers too.

	foreach ($lines2 as $line_num2 => $line2) {
		echo "<b>{$line_num2}</b> : " . htmlspecialchars($line2) . "<br />\n";
	}
	
	$myFile3 = "/etc/postfix/vmaps";
	$fh3 = fopen($myFile3, 'r');
	$theData3 = implode(', ', file($myFile3));	 
	 
	$lines3 = file('/etc/postfix/vmaps');
	echo "<br><b>Vmaps:</b><br><br>";
	// Loop through our array, show HTML source as HTML source; and line numbers too.

	foreach ($lines3 as $line_num3 => $line3) {
		echo "<b>{$line_num3}</b> : " . htmlspecialchars($line3) . "<br />\n";
	}
	
	$myFile4 = "/etc/postfix/vhosts";
	$fh4 = fopen($myFile4, 'r');
	$theData4 = implode(', ', file($myFile4));	 
	 
	$lines4 = file('/etc/postfix/vhosts');
	echo "<br><b>Vhosts:</b><br><br>";
	// Loop through our array, show HTML source as HTML source; and line numbers too.

	foreach ($lines4 as $line_num4 => $line4) {
		echo "<b>{$line_num4}</b> : " . htmlspecialchars($line4) . "<br />\n";
	}

} else
if ($page == 'newvhost') {

	print("<form method=\"post\" action=\"?page=newvhost\">");
	print("<b>Domain:</b><br /> <input type=\"text\" name=\"domain\"><br />");
	print("<input type=\"submit\" name=\"submit\" value=\"Lägg virtuel host\">");
	print("</form>");
	
	$submit = $_POST['submit'];

	if (isset($submit)) {
		$vhosts = '/etc/postfix/vhosts';
		$domain = $_POST['domain'];
		$vhosts_content = "".$domain."\n";

		// Let's make sure the file exists and is writable first.
		if (is_writable($vhosts)) {

			// In our example we're opening $vhosts in append mode.
			// The file pointer is at the bottom of the file hence
			// that's where $vhosts_content will go when we fwrite() it.
			if (!$handle = fopen($vhosts, 'a')) {
				 echo "Cannot open file ($vhosts)";
				 exit;
			}

			// Write $vhosts_content to our opened file.
			if (fwrite($handle, $vhosts_content) === FALSE) {
				echo "Cannot write to file ($vhosts)";
				exit;
			}

			echo "Success, wrote ($vhosts_content) to file ($vhosts)";

			fclose($handle);

		} else {
			echo "The file $vhosts is not writable";
		}
	}
	
	$myFile = "/etc/dovecot/users";
	$fh = fopen($myFile, 'r');
	$theData = implode(', ', file($myFile));	 
	 
	$lines = file('/etc/dovecot/users');
	echo "<br><b>Users:</b><br><br>";
	// Loop through our array, show HTML source as HTML source; and line numbers too.

	foreach ($lines as $line_num => $line) {
		echo "<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
	}
	
	$myFile2 = "/etc/dovecot/passwd";
	$fh2 = fopen($myFile2, 'r');
	$theData2 = implode(', ', file($myFile2));	 
	 
	$lines2 = file('/etc/dovecot/passwd');
	echo "<br><b>Passwd:</b><br><br>";
	// Loop through our array, show HTML source as HTML source; and line numbers too.

	foreach ($lines2 as $line_num2 => $line2) {
		echo "<b>{$line_num2}</b> : " . htmlspecialchars($line2) . "<br />\n";
	}
	
	$myFile3 = "/etc/postfix/vmaps";
	$fh3 = fopen($myFile3, 'r');
	$theData3 = implode(', ', file($myFile3));	 
	 
	$lines3 = file('/etc/postfix/vmaps');
	echo "<br><b>Vmaps:</b><br><br>";
	// Loop through our array, show HTML source as HTML source; and line numbers too.

	foreach ($lines3 as $line_num3 => $line3) {
		echo "<b>{$line_num3}</b> : " . htmlspecialchars($line3) . "<br />\n";
	}
	
	$myFile4 = "/etc/postfix/vhosts";
	$fh4 = fopen($myFile4, 'r');
	$theData4 = implode(', ', file($myFile4));	 
	 
	$lines4 = file('/etc/postfix/vhosts');
	echo "<br><b>Vhosts:</b><br><br>";
	// Loop through our array, show HTML source as HTML source; and line numbers too.

	foreach ($lines4 as $line_num4 => $line4) {
		echo "<b>{$line_num4}</b> : " . htmlspecialchars($line4) . "<br />\n";
	}

}

if ($page == 'deletevhost') {
$del_line = $_GET['line'];
	function cutline($filename,$line_no=-1) {
		$strip_return=false;

		$data=file($filename);
		$pipe=fopen($filename,'w');
		$size=count($data);

		if($line_no==-1) $skip=$size-1;
		else $skip=$line_no-1;

		for($line=0;$line<$size;$line++)
		if($line!=$skip)
		fputs($pipe,$data[$line]);
		else
		$strip_return=true;

		return $strip_return;
	}
		
		cutline("/etc/postfix/vhosts", 1);
}
?>
linx
Inlägg: 13
Blev medlem: 15 maj 2007, 02:12
Ort: Märsta
Kontakt:

Re: HowTo - mailserver med virtuella domäner och användare

Inlägg av linx »

Detta måste testas!
Tack för en utförlig howto. 8)
Skriv svar

Återgå till "Guider"