<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Werk &#187; mess</title>
	<atom:link href="http://werk.feub.net/tag/mess/feed/" rel="self" type="application/rss+xml" />
	<link>http://werk.feub.net</link>
	<description>Pourquoi Werk? ^.^</description>
	<lastBuildDate>Tue, 06 Dec 2011 14:18:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Postfix + Dovecot : utilisateurs virtuels dans une base MySQL</title>
		<link>http://werk.feub.net/2008/08/postfix-dovecot-utilisateurs-virtuels-dans-une-base-mysql/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=postfix-dovecot-utilisateurs-virtuels-dans-une-base-mysql</link>
		<comments>http://werk.feub.net/2008/08/postfix-dovecot-utilisateurs-virtuels-dans-une-base-mysql/#comments</comments>
		<pubDate>Thu, 07 Aug 2008 11:07:29 +0000</pubDate>
		<dc:creator>Fabien</dc:creator>
				<category><![CDATA[linux]]></category>
		<category><![CDATA[dovecot]]></category>
		<category><![CDATA[fedora]]></category>
		<category><![CDATA[mess]]></category>
		<category><![CDATA[messagerie]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[postfix]]></category>

		<guid isPermaLink="false">http://werk.feub.net/?p=29</guid>
		<description><![CDATA[Amélioration du serveur de messagerie]]></description>
			<content:encoded><![CDATA[<p>
Dans <a href="http://feub.net/werk/index.php/feubv9/comments/serveur_de_messagerie_imap_simple_sous_fedora_9/" title="feub.net">cet article</a>, il a été vu comment mettre en place un serveur de messagerie simple grâce au <a href="http://fr.wikipedia.org/wiki/Mail_Transfer_Agent" title="Wikipedia">MTA</a> <a href="http://www.postfix.org/" title="Postfix">Postfix</a> et à <a href="http://www.dovecot.org/" title="Dovecot">Dovecot</a> pour l&#8217;<a href="http://fr.wikipedia.org/wiki/IMAP" title="Wikipedia">IMAP</a>. Cet article va ajouter la gestion des utilisateurs virtuels (des adresses emails) dans une base de données <a href="http://www-fr.mysql.com/" title="MySQL">MySQL</a>. Cet approche sera beaucoup plus souple dans la gestion du serveur, l&#8217;ajout/édition/suppression d&#8217;une adresse se fera très simplement en ajoutant un utilisateur dans la base des utilisateurs virtuels, alors qu&#8217;auparavant, il fallait ré-éditer des fichiers texte.
</p>
<p><span id="more-29"></span></p>
<p>
L&#8217;article suivant de <a href="http://www.llaumgui.com/" title="llaumgui.com">llaumgui</a> m&#8217;a bien aidé dans la mise en place du support MySQL : <a href="http://www.llaumgui.com/post/Serveur-mail-postfix-/-postfix-mysql-/-Dovecot-/-RoundCube-/-spamassassin-sous-CentOS-5" title="llaumgui.com">http://www.llaumgui.com/post/Serveur-mail-postfix-/-postfix-mysql-/-Dovecot-/-RoundCube-/-spamassassin-sous-CentOS-5</a>
</p>
<h4>Postfix</h4>
<p>
En premier lieu, la configuration du MTA doit être modifiée pour qu&#8217;il lise les domaines et utilisateurs virtuels dans la base MySQL et non plus dans un fichier de configuration. Il est supposé que le serveur MySQL est fonctionnel et qu&#8217;il existe une base accessible pour ajouter les tables.<br />
Voici le schéma de la table <em>MX_domains</em> des domaines virtuels à créer sur le serveur MySQL :
</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">CREATE TABLE IF NOT EXISTS <span style="color: #000000; font-weight: bold;">`</span>MX_domains<span style="color: #000000; font-weight: bold;">`</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>
<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">id</span><span style="color: #000000; font-weight: bold;">`</span> tinyint<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">8</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> NOT NULL auto_increment,
<span style="color: #000000; font-weight: bold;">`</span>domain<span style="color: #000000; font-weight: bold;">`</span> varchar<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">128</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> NOT NULL,
PRIMARY KEY  <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">id</span><span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #007800;">ENGINE</span>=MyISAM  DEFAULT <span style="color: #007800;">CHARSET</span>=latin1 <span style="color: #007800;">AUTO_INCREMENT</span>=<span style="color: #000000;">2</span> ;</pre></div></div>

<p>
Et la table <em>MX_users</em> qui contiendra les informations des utilisateurs :
</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">CREATE TABLE IF NOT EXISTS <span style="color: #000000; font-weight: bold;">`</span>MX_users<span style="color: #000000; font-weight: bold;">`</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span>
<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">id</span><span style="color: #000000; font-weight: bold;">`</span> tinyint<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">8</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> NOT NULL auto_increment,
<span style="color: #000000; font-weight: bold;">`</span>email<span style="color: #000000; font-weight: bold;">`</span> varchar<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">128</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> NOT NULL,
<span style="color: #000000; font-weight: bold;">`</span>password<span style="color: #000000; font-weight: bold;">`</span> varchar<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">128</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> NOT NULL,
<span style="color: #000000; font-weight: bold;">`</span>name<span style="color: #000000; font-weight: bold;">`</span> varchar<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">128</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> NOT NULL default <span style="color: #ff0000;">''</span>,
<span style="color: #000000; font-weight: bold;">`</span>uid<span style="color: #000000; font-weight: bold;">`</span> smallint<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">5</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> unsigned NOT NULL default <span style="color: #ff0000;">'1000'</span>,
<span style="color: #000000; font-weight: bold;">`</span>gid<span style="color: #000000; font-weight: bold;">`</span> smallint<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">5</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> unsigned NOT NULL default <span style="color: #ff0000;">'1000'</span>,
<span style="color: #000000; font-weight: bold;">`</span>domain<span style="color: #000000; font-weight: bold;">`</span> varchar<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">128</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> NOT NULL default <span style="color: #ff0000;">''</span>,
<span style="color: #000000; font-weight: bold;">`</span>maildir<span style="color: #000000; font-weight: bold;">`</span> varchar<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">255</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> NOT NULL default <span style="color: #ff0000;">''</span>,
<span style="color: #000000; font-weight: bold;">`</span>is_imap<span style="color: #000000; font-weight: bold;">`</span> tinyint<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> unsigned NOT NULL default <span style="color: #ff0000;">'1'</span>,
<span style="color: #000000; font-weight: bold;">`</span>is_active<span style="color: #000000; font-weight: bold;">`</span> tinyint<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> NOT NULL default <span style="color: #ff0000;">'1'</span>,
PRIMARY KEY  <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">id</span><span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>,
UNIQUE KEY <span style="color: #000000; font-weight: bold;">`</span>address<span style="color: #000000; font-weight: bold;">`</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">`</span>email<span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>,
KEY <span style="color: #000000; font-weight: bold;">`</span>domain<span style="color: #000000; font-weight: bold;">`</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">`</span>domain<span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #007800;">ENGINE</span>=MyISAM  DEFAULT <span style="color: #007800;">CHARSET</span>=latin1 <span style="color: #007800;">AUTO_INCREMENT</span>=<span style="color: #000000;">4</span> ;</pre></div></div>

<p>
A noter que le champ <em>email</em> désigne la partie de l&#8217;adresse email avant l&#8217;arobase, par exemple pour toto@exemple.net, il faudra renseigner seulement <em>toto</em> dans le champ <em>email</em>. Le mot de passe quand à lui sera un hash <a href="http://fr.wikipedia.org/wiki/MD5" title="Wikipedia">MD5</a> dans la base.
</p>
<p>
Maintenant, il faut éditer le fichier <em>main.cf</em> de configuration de Postfix, en changeant ces deux lignes :
</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">virtual_mailbox_domains = <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>postfix<span style="color: #000000; font-weight: bold;">/</span>domains
virtual_mailbox_maps = <span style="color: #7a0874; font-weight: bold;">hash</span>:<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>postfix<span style="color: #000000; font-weight: bold;">/</span>user_mailboxes_path</pre></div></div>

<p>
Par :
</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">virtual_mailbox_domains = mysql:<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>postfix<span style="color: #000000; font-weight: bold;">/</span>mysql_virtual_domains.cf
virtual_mailbox_maps = mysql:<span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>postfix<span style="color: #000000; font-weight: bold;">/</span>mysql_virtual_mailboxes.cf</pre></div></div>

<p>
Le préfixe <em>mysql</em> indique bien que les données vont être extraites de la base de données relationel. Mais les<br />
deux fichiers <em>mysql_virtual_domains.cf</em> et <em>mysql_virtual_mailboxes.cf</em> correspondant contenant les<br />
requêtes SQL ne sont pas encore crées. Les voici :
</p>
<p>mysql_virtual_domains.cf</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">user = dbuser
password = dbpassword
dbname = mailserver
hosts = localhost
query = SELECT <span style="color: #c20cb9; font-weight: bold;">id</span> FROM MX_domains WHERE <span style="color: #007800;">domain</span>=<span style="color: #ff0000;">'%s'</span></pre></div></div>

<p>mysql_virtual_mailboxes.cf</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">user = dbuser
password = dbpassword
dbname = mailserver
hosts = localhost
query = SELECT maildir FROM MX_users WHERE <span style="color: #007800;">email</span>=<span style="color: #ff0000;">'%s'</span> AND <span style="color: #007800;">is_active</span>=<span style="color: #000000;">1</span></pre></div></div>

<p>
Postfix peut être redémarré :
</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># /etc/init.d/postfix restart</span></pre></div></div>

<h4>Dovecot</h4>
<p>
La première chose à faire est d&#8217;ajouter le module MySQL pour Dovecot, sous <a href="http://www.fedora-fr.org/" title="Fedora">Fedora</a> :
</p>
<pre lang=bash">
# yum install dovecot-mysql
</pre>
<p>
En ce qui concerne sa configuration, les manipulations sont un peu similaires, à savoir, créer des fichiers de requêtes SQL pour<br />
que le serveur IMAP ramène les bonnes données de la base et non plus d&#8217;un fichier plat. Voici la partie qui va être<br />
modifiée du fichier <em>/etc/dovecot.conf</em> pour passer de :
</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mechanisms = plain
passdb passwd-file <span style="color: #7a0874; font-weight: bold;">&#123;</span>
args = <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>postfix<span style="color: #000000; font-weight: bold;">/</span>dovecot<span style="color: #000000; font-weight: bold;">/</span>users.conf
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>
A ceci :
</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mechanisms = plain <span style="color: #c20cb9; font-weight: bold;">login</span> digest-md5 cram-md5
passdb sql <span style="color: #7a0874; font-weight: bold;">&#123;</span>
args = <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>postfix<span style="color: #000000; font-weight: bold;">/</span>dovecot<span style="color: #000000; font-weight: bold;">/</span>mysql_users.cf
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>
On note l&#8217;ajout des mécanismes avec hash : digest-md5 (je n&#8217;avais mis que la gestion de mot de passe en texte brut dans l&#8217;article précédent). Ainsi que la vérification du mot de passe qui ne se fait plus par un fichier mais par une requête SQL dans le fichier <em>mysql_users.cf</em> sous /etc/postfix/dovecot/ (choix arbitraire), comme suit :
</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">driver = mysql
connect = <span style="color: #007800;">host</span>=localhost <span style="color: #007800;">dbname</span>=mailserver <span style="color: #007800;">user</span>=dbuser <span style="color: #007800;">password</span>=dbpassword
default_pass_scheme = PLAIN-MD5
password_query = SELECT password FROM MX_users \
WHERE email = <span style="color: #ff0000;">'%n'</span> AND is_active = <span style="color: #000000;">1</span>
user_query = SELECT concat<span style="color: #7a0874; font-weight: bold;">&#40;</span>email, <span style="color: #ff0000;">'@'</span>, domain<span style="color: #7a0874; font-weight: bold;">&#41;</span> AS user \
FROM MX_users WHERE email = <span style="color: #ff0000;">'%n'</span></pre></div></div>

<p>
Dovecot veut l&#8217;adresse complète comme nom d&#8217;utilisateur, donc on utilise la fonction concat() de MySQL pour la construire.<br />
A noter que la partie <em>userdb</em> pourrait être dans une table également, mais elle est laissée comme telle en <em>static</em>, car l&#8217;emplacement des messages ne bougera pas dans ce cas-ci, ils seront toujours sous <em>/home/vmail/domaine/utilisateur</em> et seront crées automatiquement lors d&#8217;ajout de nouvelles boîtes virtuelles :
</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">userdb static <span style="color: #7a0874; font-weight: bold;">&#123;</span>
<span style="color: #666666; font-style: italic;"># Chemin vers les BAL</span>
args = <span style="color: #007800;">uid</span>=vmail <span style="color: #007800;">gid</span>=vmail <span style="color: #007800;">home</span>=<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>vmail<span style="color: #000000; font-weight: bold;">/%</span>d<span style="color: #000000; font-weight: bold;">/%</span>n<span style="color: #000000; font-weight: bold;">/</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>
Redémarrage de Dovecot :
</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># /etc/init.d/dovecot restart</span></pre></div></div>

<p>
Tout devrait fonctionner maintenant. Pour ajouter/supprimer/éditer un utilisateur, il suffit de le faire dans la base de données, par exemple par l&#8217;intermédiaire de <a href="http://www.phpmyadmin.net/" title="phpMyAdmin">phpMyAdmin</a> ou d&#8217;un script <a href="http://fr.wikipedia.org/wiki/Bourne-Again_shell" title="Wikipedia">bash</a> (ou autre langage) qui fera peut-être l&#8217;objet d&#8217;un prochain article.
</p>
<p>
Note : les anti-slash \ ne sont pas à taper, ils indiquent que j&#8217;ai coupé les lignes pour que ça ne dépasse pas du cadre. Il s&#8217;agit d&#8217;une seule et même ligne.
</p>
<div class="related-links">
<h4>
Article en relation : <strong>Serveur de messagerie</strong> :<br />
</h4>
<ul>
<li><a href="http://feub.net/werk/index.php/feubv9/comments/serveur_de_messagerie_imap_simple_sous_fedora_9/" title="feub.net">Serveur de messagerie IMAP simple sous Fedora_9</a></li>
</ul>
</div>
]]></content:encoded>
			<wfw:commentRss>http://werk.feub.net/2008/08/postfix-dovecot-utilisateurs-virtuels-dans-une-base-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using apc (Feed is rejected)
Page Caching using apc
Object Caching 214/225 objects using memcached

Served from: werk.feub.net @ 2012-02-09 00:16:42 -->
