PHP + Apache + mod_fcgid + suexec on Ubuntu and Mac

Posted: August 14th, 2014 | Author: | Filed under: General | No Comments »

Background

On linux machines (Mac too) the default installation of Apache+PHP is sometime not suitable for modern script due to at least two reasons:

      1. speed
      2. user rights

While the first is obvious, the second one needs some explanations and the best way is by giving a real-world example. In the Symfony framework the content is prepared under a "cache" folder for achieving a very good speed in processing requests. The framework also uses some command prompt statements for achieving various tasks such as database structure update, warming up the cache, etc. As one knows, all requests processed by Apache runs by default under its user, be it "apache", "www" or "nobody". On the other hand the command line statements run under the current logged-in user, let's call it "myuser". Both actions above uses the same cache folder and here is where the conflict appears, since one cannot update the other's files. Hence the need to run the script under the same user, both withing the Apache and from the command prompt. Plus that is faster (developers would like this).

Goal & solution

One recommended configuration for achieving the speed is to replace the standard mod_php with mod_fcgid, meaning that the process won't run anymore inside Apache but as an external process called by Apache when needed.

As for running the process as another user than Apache's, there is suexec which consists of a module running under apache and a binary executable acting as a wrapper.

The configuration presented here uses virtual hosts, a know feature of Apache letting one run multiple websites on the same computer, each with its own configuration, folder and user account. The process of configuring the server is split in two parts, first for configuring the mod_fcgid module then adding suexec mechanism.

Configuring mod_fcgid

The configuration starts by disabling the standard php module and installing the mod_fcgid module. The steps differs based on the linux distribution therefore the steps below will be given for Ubuntu and Mac so the reader will have to figure out how it works for other ditributions.

Ubuntu:

sudo apt-get install libapache2-mod-fcgid php5 php5-cgi

Note that this article assume that apache is already installed. If php5 is also installed is even better, just be sure php5-cgi is also.

Mac

sudo port install php5 +fastcgi

<span style="color: #c20cb9; font-weight: bold;">sudo</span> port <span style="color: #c20cb9; font-weight: bold;">install</span> mod_fcgid

Then disable mod_php end enable mod_fcgid:

Ubuntu:

sudo a2dismod php5
sudo a2enmod fcgid

Mac:

sudo vi /private/etc/apache2/httpd.conf

Comment this line: #LoadModule php5_module libexec/apache2/libphp5.so

Add/uncomment this line: LoadModule fcgid_module libexec/apache2/mod_fcgid.so

Add this line to your php.ini (Ubuntu: /etc/php5/cgi/php.ini, Mac: /etc/php.ini)

cgi.fix_pathinfo=1

 

Tip: the location of php.ini differs and not only that, there may be two different files, one for PHP as module and the other for PHP as CGI. Locate them in the system with:

locate php.ini

A better approach can be to use php itself to tell where is its ini file:

php -i |grep php.ini

Result:

Loaded Configuration File => /etc/php.ini

Additional useful information, in case one needs to know what PHP version runs from command line, especially where there are two PHP versions installed:

#which php
/usr/bin/php

#php -v

PHP 5.4.24 (cli) (built: Jan 19 2014 21:32:15)

 

Now is the time to configure mod_fcgid and this is done in two places: one global place inside apache's own confguration files and the other in each site. The global configuration file looks like this:

Ubuntu (/etc/apache2/mods-enabled/fcgid.conf):

<IfModule mod_fcgid.c>
AddHandler    fcgid-script .fcgi
FcgidConnectTimeout 20
FcgidFixPathinfo 1
FcgidMaxRequestLen 2097152
</IfModule>

Mac (/etc/apache2/other/fcgid.conf):

<IfModule mod_fcgid.c>
FcgidIPCDir /usr/lib/apache2/fcgid/sock
FcgidProcessTableFile /usr/lib/apache2/fcgid/fcgid_shm
FcgidConnectTimeout 20
FcgidFixPathinfo 1

# fix for:   mod_fcgid: read data timeout in 40 seconds
FcgidIdleTimeout 3000
FcgidMinProcessesPerClass 100
FcgidConnectTimeout 120
FcgidIOTimeout 400
FcgidMaxProcesses 150
FcgidBusyTimeout 600

# to get around upload errors when uploading images increase the MaxRequestLen size to 15MB
FcgidMaxRequestLen 15728640

FcgidProcessLifeTime 7200
</IfModule>

Although the Mac configuration seems more complex, it is only because more configuration directives have been used. Normally the two may look the same, except the paths of FcgidIPCDir and FcgidProcessTableFile

Tip: Be aware that the names of the directives were changed since mod_fcgid 2.3.2  (it is currently at version 2.3.9). The reason you're warned here is because many answers online uses PHP_Fix_Pathinfo_Enable instead of FcgidFixPathinfo. (all changes in the table here). The most important directives to use are: FcgidFixPathinfo, FcgidMaxRequestLen and FcgidBusyTimeout.

 

The site configuration file should include the followings somewhere inside <VirtualHost> tags:

Ubuntu (/etc/apache2/sites-available/yoursite.com.conf) & Mac (/etc/apache2/extra/httpd-vhosts.conf):

<IfModule mod_fcgid.c>
#SuexecUserGroup myuser mygroup
<Directory /var/www/yoursite.com/ >
Options +ExecCGI
AllowOverride All
AddHandler fcgid-script .php
FcgidWrapper /var/www/php-fcgi-scripts/php-fcgi-starter .php
Order allow,deny
Allow from all
</Directory>
</IfModule>

 

Tip: Note that in the above configuration the line with SuexecUserGroup had been commented. This is to avoid seeing errors caused by Suexec while setting up fcgid. Later the line will be un-commented. Or, if you feel lucky, un-comment it now.

Once the above are set, restart apache:

Ubuntu: sudo service apache2 restart

Mac: sudo apachectl restart

 

Now is is time to talk about the log files because they tell valuable information about the problems which may occur.

Apache's log file to watch is named "error_log" and there you may find issues related to why the server does not start, when some SSL configuration is broken or when suexec has issues.

Suexec has its own log in the same folder as Apache's and there you will find issues related to user rights or problems when running the executable.

Each site is recommended to have its own access and error log files. There you will find errors related to the script or accessing the script, acting as a starting point in solving the issues. In case of errors related to suexec you will find here only some indications, while the details have to be found in the suexec file above

Configuring suexec

This executable binary usually comes with Apache but on a Mac installation this binary was missing. With or without it, sometimes is necessary to adjust its default settings, e.g. those it has been compiled with. Below all these situations will be covered so one can skip some steps if the default installation fits the needs.

Begin with checking the location end existence of suexec:

# which suexec

If the binary is not in the path you won't get any result. Try this:

# locate suexec

The output should be a path like /usr/lib/apache2/suexec on Ubuntu or /usr/bin/suexec on Mac, but the location may vary.

Once you located it, run it to see its current settings. Since it is supposed to be run by root only, you need to use "sudo":

Ubuntu:

# sudo /usr/lib/apache2/suexec -V
-D AP_DOC_ROOT="/var/www"
-D AP_GID_MIN=100
-D AP_HTTPD_USER="www-data"
-D AP_LOG_EXEC="/var/log/apache2/suexec.log"
-D AP_SAFE_PATH="/usr/local/bin:/usr/bin:/bin"
-D AP_UID_MIN=100
-D AP_USERDIR_SUFFIX="public_html"

Mac:

#sudo /usr/bin/suexec -V
Password:
-D AP_DOC_ROOT="/Users"
-D AP_GID_MIN=10
-D AP_HTTPD_USER="_www"
-D AP_LOG_EXEC="/var/log/apache2/suexec_log"
-D AP_SAFE_PATH="/usr/local/bin:/usr/bin:/bin"
-D AP_UID_MIN=500
-D AP_USERDIR_SUFFIX="Sites"

Pay attention to DOC_ROOT : it is the only path allowed to run executable (wrapper) from. The HTTPD_USER is useful in verifying that the wrapper is accessible by this user Finally, UID and GID are constraints for minimum user and group IDs, as they are defined in the /etc/passwd file.

The wrapper file is being launched by Apache for any request whose extension ends in PHP, as defined in the site's configuration file above and suexec must have the rights to access it. Also, the file must be inside DOC_ROOT. The file looks like this:

#!/bin/sh
PHPRC=/etc/php5/cgi/
export PHPRC
export PHP_FCGI_MAX_REQUESTS=5000
export PHP_FCGI_CHILDREN=8
exec /usr/lib/cgi-bin/php

Tip: PHPRC is the path up to php.ini. The executable on the last line must be the cgi version of php binary. Sometimes (on Mac) it is called "php-cgi" and is under /opt/local/bin/. Just check yours using "which" or "locate" and run it as "php-cgi -v" to verify that it is ok and has the expected version.

So, create the file under DOC_ROOT, let's say "/var/www/php-fcgi-scripts/php-fcgi-starter" with the above content and make it executable (chmod a+x /var/www/php-fcgi-scripts/php-fcgi-starter). Take care yourself of creating the folder php-fcgi-scripts and assigning it the proper user and group (myuser and mygroup).

Tip: an easy way to verify that a user can access that file is to add "-v" as parameter for php on the last line then call it with:

su myuser -c "/var/www/php-fcgi-scripts/php-fcgi-starter"

In other words: "run this command as user myuser" and it will display the PHP version. Remove the parameter afterward. Repeat: remove the parameter afterward (so it will run whatever PHP file will be send by Apache as parameter)

 

Back on track now, if everything goes well you should remove the comment to SuexecUserGroup and restart Apache. Prepare a simple PHP file in the site's root folder with the following content:

<?php phpinfo();

If everything was setup successfully then you'll get the nice long listing with PHP's current configuration. Among those lines you will find references to mod_fcgid and all other features configured.

 

Notes from author

I wanted to put these notes online as a compilation of multiple installations I made with this particular configuration on different linux distributions because I spent many hours finding information and solving all sort of issues myself. If the article contains errors, please let me know. If needs completion, please let me know. I hope that you will use the time you'll save here to teach someone else something.

 

 


Setup of Mercurial server with cPanel – the easy way

Posted: March 23rd, 2012 | Author: | Filed under: General | No Comments »

Mercurial is a cross-platform, distributed revision control tool for software developers. In this article will be presented a handy way to install and configure Mercurial on a server with cPanel . All operations presented will only need a standard FTP/SSH access to a user account, except the initial installation of the Mercurial RPM which needs root rights.  This installation procedure is very suitable for those creating websites and wanting a quick solution to track the changes in their source code using a repository stored right next to the website.

 

The information below were compiled from several sources with some modifications to suit the above needs.  The aim is to use only cPanel and SSH with user rights (e.g. no root required !)  and have a repository ready in 5 minutes.  The repository will then be accessed securely as hg.yoursite.com

 

Intial preparation:   install the Mercurial package as root http://mercurial.selenic.com/wiki/HgWebDirStepByStep

Here is a sample installation from a RPM on an OpenSuse linux:

 wget http://download.opensuse.org/repositories/devel:/tools:/scm/openSUSE_10.2/x86_64/mercurial-1.0-1.2.x86_64.rpm
$ sudo rpm -ihv mercurial-1.0-1.2.x86_64.rpm

 

Step 1.  Create subdomain

Login in cPanel and create hg.yourdomain.com.  Do not create the directory under /public_html  so the repository won't mix with the files of the website

 

Step 2. Create the configuration file of the repository

Use a FTP client and navigate into the directory of the subdomain. Create a folder for your repo, i.e. projectrepo . Use some name other than the one you want to access the repo with because later there will be some URL rewiting rules added into a .htaccess file which won't work otherwise.  In this example the repo url will be made as hg.yourdomain.com/project

Continue navigating into cgi-bin where two files has to be be uploaded: hgweb.config containing the repository configuration and hgwebdir.cgi containing the code needed to run the repository.  Create the two files on your computer and upload them into /cgi-bin:

/cgi-bin/hgweb.config

[paths]
#VIRTUAL_PATH = /REAL/PATH
project = ~/hg/projectrepo

/cgi-bin/hgwebdir.cgi

#!/usr/bin/env python
from mercurial import demandimport; demandimport.enable()

import cgitb
cgitb.enable()

import os
os.environ["HGENCODING"] = "UTF-8"

from mercurial.hgweb.hgwebdir_mod import hgwebdir
import mercurial.hgweb.wsgicgi as wsgicgi

application = hgwebdir('hgweb.config')
wsgicgi.launch(application)

Change the attributes of hgwebdir.cgi to be executable (0755).  This can be made within the FTP client (at least TotalCommander implements it and probably many other such applications have that option. It is under Files->Change attributes).

 

Step 3. Initialize repository

Using a SSH console go into the ~/hg folder and run these commands:

hg init

find . -type f -exec chmod 0666 "{}" \;

find . -type d -exec chmod 0777 "{}" \;

 

The last two commands are needed when the web server (Apache) runs the scripts under a different user than the owner of the account.  In order to create files and folder full write rights are needed for user apache or nobody.

A configuration file will have to be created and uploaded into the new repository. For this , create a file named hgrc with the following content:

~/hg/project/.hg/hgrc

[web]
description = My repository
push_ssl = false
allow_push = john

# enable snapshot downloads
allow_archive = gz zip bz2

baseurl = /

In this configuration the ssl is disabled which may be a security problem. If your host allows https connections then you may set this to true later. However, the repository is protected through HTTP Basic authentication – see below.

Upload the file above under ~/hg/project/.hg/

 

 Step 4. Secure the repository

Create a .htaccess file with the following content for uploading in the root of your subdomain:

~/hg/.htaccess

# Taken from http://www.pmwiki.org/wiki/Cookbook/CleanUrls#samedir
# Used at http://ggap.sf.net/hg/
Options +ExecCGI
RewriteEngine On
#write base depending on where the base url lives
RewriteBase /cgi-bin
RewriteRule ^$ hgwebdir.cgi  [L]

# Send requests for files that exist to those files.
RewriteCond %{REQUEST_FILENAME} !-f
# Send requests for directories that exist to those directories.
RewriteCond %{REQUEST_FILENAME} !-d
# Send requests to hgwebdir.cgi, appending the rest of url.
RewriteRule (.*) /cgi-bin/hgwebdir.cgi/$1  [QSA,L]

The rules above allows accessing the repository with an url like hg.yourdomain.com/project instead of hg.yourdomain.com/cgi-bin/hgwebdir.cgi/project and is a replacement for the original procedure described in the Mercurial documentation where one has to modify httpd.conf, as root.

Finally, password protect the repository within cpanel. The operations below will update .htaccess, adding the necessary lines for basic authentication:

 

Finish

Access your repository within a browser with an URL like:

http://john:sompassword123@hg.yourdomain.com/hg/project

Use the URL above in your Mercurial client too.

 

 Referrences


Postfix with DKIM, Domainkeys, SPf and Sender-ID

Posted: April 3rd, 2010 | Author: | Filed under: Server side | Tags: , , , , | 11 Comments »

The authentication methods are a great tool for fighting against spammers and every server admin should have it implemented on their systems. But since it is not that simple to configure them, they often prove to be a headache with not enough documentation available. I had to pay myself the tribute (in hours spent) for making the system work in the configuration I wanted and for the others having the same problems here is my story.My configuration is : CentOS 5, Postfix 2.3.3

The server is running some websites and I wanted to be capable of sending signed emails within PHP and also using some email client through SMTP. Since there are more than one website I wanted also a simple way to add/modify domains in the filters' configuration.

Whenever I write "domain.tld" you should use your own domain name (mysite.com, wikitiki.org, etc)

The tools I installed are listed here under the Authentication section. They are:

  • dkfilter - a SMTP-proxy signing filter. Quite easy to install but, as the author says, it is good for sending authenticated email served by the port 587 not 25 which is why I give up on it. My server must be able to send emails from PHP and act as a SMTP mail server for another server.
  • dkim-milter – DKIM signer/verifier (http://sourceforge.net/projects/dkim-milter/)
  • dk-milter – Domainkeys sender/verifier. (http://sourceforge.net/projects/dk-milter/) Pay attention to these names because it is a lot of confusion because not only DK and DKIM sounds alike but their executable are called "filters": dkim-filter and dk-filter. Couldn't be much more complicated for newbies.
  • sid-milter – Sender ID verifier. Note that it's a verifier only ! (http://sourceforge.net/projects/sid-milter/)

1. DKIM

Installing dkim. For this follow one of these links:

How to Setup DomainKeys Identified Mail (DKIM) with Postfix and Ubuntu Server

Setting up DKIM, SPF, Domainkeys DNS, Regular DNS on CentOS 5.3

I won't go through the installation steps you can read about in tutorials as above. Instead I will focus on what should be checked, customized, verified so that will help in custom installations or troubleshooting.

So, for the installation I will focus on:

subject value description
username/usergroup domainkeys.domainkeys This is the owner of the signer/verifier processes. Doesn't matter the names but once created will be used for all filters
folders for configuration /etc/mail/dkim/
/etc/mail/domainkeys/
These must be owned by the user above. When adding new domains I have to modify some files here
initialization scripts /etc/init.d/dkim
/etc/init.d/domainkeys
/etc/init.d/sender-id
Essentially is the same start-up script configured for each filter. Good to know where they are when testing the configuration
folders for private keys /var/db/domainkeys/domain.tld/ These must be owned by the user above. Here I'll place the private keys
folders & files for processes' PIDs /var/run/dkim-milter/pid
/var/run/dk-milter/pid
/var/run/dk-milter/pid
These also have to be owned by the user above. The filters will create/remove the "pid" files as they need

OK, so here there are some background information. DKIM and Domainkeys uses two DNS records and a private key to sign the emails. The DNS records are

  1. policy record. It is defined as a TXT record :
    _domainkey.domain.tld IN TXT "t=y;o=~"

    t=y means the domain is in test mode. When everything works it can be changed to "n"

    o=~ means that some emails will be signed. "o=-" means that all domains are signed

  2. selector record. Also a TXT record storing the key and other parameters:
    default._domainkey.domain.tld IN TXT  "g=*; k=rsa; p=MIGfMA0...ABC"

    "default" is the selector name and will be the same as the one used for generating the keys – see below

    "_domainkey" is a keyword preceding the domain name

    "k=" key type. Optional. Default is rsa

    "p=" public key data.

    "g=" granularity of the key. Optional, default is "*"

    "v=DKIM1" version. It is recommended but I got some errors when I tried to use the same record for Domainkeys because it is defined for DKIM only so I did not use it.  Here is the yahoo tool I tested with

Note: in the two dns records above I used "domain.tld" only because my external nameserver service required it in its web interface. If you own the namesever you will probably not use it as will be appended automatically by named/bind

Important : the server may sign ALL emails with its own domain name or may sign it with other domains but in this case all other domains must have the two DNS records maintained. I went for the first option.

Generating the keys

there are three ways to generate them:

  • online Domain Key / DKIM Key Generation Wizard
  • basic:
    openssl genrsa -out private.key 1024
    openssl rsa -in private.key -out public.key -pubout -outform PEM
  • script included with the dkim:
    dkim-genkey -r -d domain.tld

The result will always be two files, one private key to copy into /var/db/domainkeys/domain.tld/default and one to use it in the DNS record as value for "p=" parameter. Pay attention that I used "default" as filename for the private key in order to use that file for a selector named "default" and defined as such in the DNS record. It is possible to have different selectors which would handle different emails or domains and so I can have:

/var/db/domainkeys/domain.tld/m1.pem – this is created for the selectod m1 and I added an extension just to remember it is a private key file. the extension, if exists, must be "pem" or "private".

/var/db/domainkeys/otherdomain.tld/default – this one will serve emails for another domain with another selector named "default"

The init script

The init script will start|stop the filter and there is where I put the command to do that. It may have two forms, either with the configuration given as parameters to the executable app or with it uses a single parameter which points to a configuration file containing all settings. I used the 2nd option:

COMMAND="dkim-filter -x /etc/dkim.conf"

Then I put in the dkim.conf file all the configuration parameters I needed. Don't forget to make it readable by the user defined above (domainkeys). Some of the most important parameters I put there are:

KeyList                 /etc/mail/dkim/keylist
MTA                     MSA
Selector                default
UserID                  domainkeys:domainkeys
Socket                  inet:8891@localhost

Don't forget that the port numbers will be used in your /etc/postfix/main.cf file:

smtpd_milters = inet:localhost:9967,inet:localhost:8891,inet:localhost:8892
non_smtpd_milters = inet:localhost:9967,inet:localhost:8891,inet:localhost:8892
milter_protocol = 2
[[code]]czozMDpcIm1pbHRlcl9kZWZhdWx0X2FjdGlvbiA9IGFjY2VwdFwiO3tbJiomXX0=[[/code]]

I have here 3 filters, dkim, domainkeys and sender-id, each on its own port. You will find below the entire configuration file I created.

The keylist file

the keylist file contains the relation between the sender-pattern, the signing domain and the path to the private key. I use this keylist file in order to manage easier the domains on my webhost server. For a singe domain one would only use the dkim's configuration file. My keylist file's location is defined in the configuration :

KeyList /etc/mail/dkim/keylist

And contains :

*:domain.tld:/var/db/domainkeys/domain.tld/default

..meaning: all emails (*) will be signed with the domain "domain.tld" using the private key from the file "/var/…/default".

The selector used in  the signature will be the filename portion of  keypath. If the  file referenced by keypath cannot be opened, the  filter will try  again by appending ".pem" and then ".private" before giving up. (man dkim-filter.conf)

Useful commands, part I

  • postsuper -d ALL

    Delete all mail queue. Sometimes due to too many unsent emails the logs are filled in continuously by the emails in queue. For a clean start it's good to have an empty mail queue. Be careful on a production server (you may just ignore it)
  • mail myaccount@yahoo.com

    Send emails from command line. good for a quick test. Verify the results on a yahoo account for both DKIM and Domainkeys
  • telnet localhost 25
    EHLO domain.tld
    MAIL FROM: root@domain.tld
    RCPT TO: myaccount@yahoo.com
    data
    bla bla
    .
    quit

    This is a suite of commands to test sending email through SMTP. Or you can use an email client set for sending through SMTP protocol
  • php -r "mail('myaccount@yahoo.com','test mail','bla bla','From: root@domain.tld');"

    Let the email be sent within PHP. This will test PHP mail() configuration

    A note here: At a certain point my mail sent from PHP was not signed with Domainkeys, only with DKIM. After several tests I found that the $headers in my php script were using Windows separator for new like (\r\n) instead of Linux (\n). I never thought it matters in headers !

  • Online tools: DomainKey Implementor's Tools and Library for email servers & clients especially "DomainKey Policy Record Tester." and "DomainKey Selector Record Tester."
  • tail -f /var/log/maillog

    this is where I look for errors

2. Domainkeys

As above, start with the download & installation from the given links from sourceforge or with the ones below:

Quick And Easy Setup For DomainKeys Using Ubuntu, Postfix And Dkim-Filter

man dk-filter – DomainKeys filter for sendmail online version of the man page for dk-filter

The DNS records follows the same rules as the DKIM above. In fact both DKIM and Domainkeys will use the same DNS records

The init script

The dk-filter does not support all parameters supported by DKIM and especially the one which would allow me to put all configuration in one file therefore the command from this initialization script has all parameters in one line:

COMMAND="dk-filter -u $USER -p inet:$PORT@localhost -l -P $PIDFILE -d $DOMAIN -S $SELECTOR -c simple -k -s $KEYFILE -i $IFILE -m $SUBMISSION_DAEMON -D"
Parameters:

  • -u userid it runs under
  • -p socket to be used by the filter for allowing connections from postfix. It must use a different port number than dkim (of course)
  • -l log calls to syslog
  • -P pidfile – the file used to write the process ID into
  • -d domain list or path to a file containing the domain list one per line (wildcard * allowed in the domain name )
  • -S selector, the name of the selector to be used
  • -k Causes -s to be interpreted as the location of a key list
  • -s keyfile or keyfilepath. Since -k is used this one will be a path to my keyfile (/etc/mail/domainkeys/keylist)
  • -i a file of internal hosts whose mail should be signed rather than verified (/etc/mail/domainkeys/ilist)
  • -D Sign subdomains too

The content of the keylist (differs from DKIM's !):

*@domain.tld:/var/db/domainkeys/domain.tld/default

The ilist contains some IPs (local network's):
192.168.1.0/24127.0.0.1

Note that I am using "/etc/mail/dkim/trusted-hosts" as value for the -d (domain) parameter. This is a path pointing to a dkim folder because I want to make as little changes as possible when adding or removing a domain so I use it for both filters.

The keylist file

This is another small difference than the DKIM's keylist file content. It looks like this (/etc/mail/domainkeys/keylist) :

*@domain.tld:/var/db/domainkeys/domain.tld/default

Each line is in the form "sender-pattern:keypath" where sender-pattern is compared against the sender and keypath is the path to the private key file. The filename portion of this keypath is also the selector name

Some problems I run into

At some moment I got in maillog : "can't read SMFIC_EOH reply packet header: Success" which was solved after setting the SUBMISSION_DAEMON above as MSA (instead of smtp as I saw somewhere)

Playing with milter_protocol=4 in /etc/postfix/main.cf I got "can't read SMFIC_DATA reply packet header: Success" which was because I've given a value which was not supported. Also milter_protocol=6 thrown error so I let it set on : milter_protocol = 2

Useful commands, part II

  • postconf -d

    Display the default postfix configuration
  • postconf -n

    Display the non-default parameters of the postfix configuration
  • /usr/sbin/sendmail -t -f root@domain.tld myaccount@yahoo.com
    From: root@domain.tld
    Subject: test it
    bla bla
    .

    Test sending email using the sendmail command as defined in the php.ini . Note that php.ini has an additional parameter for sendmail, "-i" which suppress the use of a dot as "end of input" data.
  • ps aux |grep dk

    This should confirm that the filters are running (dk-filter and dkim-filter). It was useful to check this because when I got errors from filters they had just ended silently.

3. Sender-id

This would be the 3rd filter installed and is aimed for hotmail accounts. There is a confusion between Sender-ID and SPF because they both use the same DNS registration record but the implementation of the Sender-ID protocol is similar with DKIM above. Read more about SPF vs Sender ID

Note that in order to sign the emails correctly one would only need the SPF record – see next section. At least this should work for 80% of the emails sent to hotmail/MSN according to the link above.

The init script

I named the init script "/etc/init.d/sender-id" and the command used to start the filter is:

COMMAND="sid-filter -u $USER -t -p inet:$PORT@localhost -l -P $PIDFILE -d $DOMAIN -D -h"

The parameters means:

  • -u username to run under
  • -t test mode (never reject messages)
  • -p socketspecs the socket for postfix to communicate with
  • -l log via syslog (/var/log/maillog)
  • -P the filename which will store the PID of the current process
  • -d a list of domains whose emails should be ignored by this filter.
    Warning : this is a list of the domains to be ignored so if you want to see this filter's signature in the header of your emails then use an empty value for DOMAIN. Somehow strange in my opinion.
  • -D the way to handle the DNS errors (soft failures)
  • -h adds a header indicating the presence of this filter

4. SPF

The SPF authentication is used by google and is quite simple requiring no software installation on the server. In order to generate a SPF record for your domain you will probably have to go to the most-used tool :

Setup wizard to create SPF records for your domain

The record it will create should be placed as a TXT record at your domain and looks like this:

v=spf1 a a:external.domain.tld ~all

Where:

  • "v=spf1" is the SPF protocol version
  • "a" mechanism matching current domain (e.g. my own domain.tld)
  • "a:external.domain.tld" mechanism matching an external domain which may use my own domain.tld to send emails
  • "mx" mechanism matches the current domain's MX records. Useful if I want to have a secondary server act only as mail server
  • "all" mechanism matches everything else. It usually stay at the end. The character which prefixes this mechanism means "SoftFail" and it only indicates that all other domains than the one defined won't be recognizable by this SPF record

Definition: Mechanisms can be used to describe the set of hosts which are designated outbound mailers for the domain.

Observation: the SPF standard specify that there should be used the new DNS registration record type SPF instead of TXT but because of compatibility with the current DNS implementations it is recommended to put both TXT and SPF records with the same content.

Useful commands, part III

Here are some of the tools and commands I used to verify that the record is in place and the configurations above are working:

  • dig domain.tld TXT

    This would query the nameserver for the TXT record and should return something like:
    ;; ANSWER SECTION:
    domain.tld.             300     IN      TXT     "v=spf1 a a:external.domain.tld ~all"
  • Send an email with an empty subject to check-auth@verifier.port25.com . I strongly recommend using an email client because sending from comand prompt resulted sometime in failed Domainkey verification due to probably some extra characters added into the headers. The result will arrive in email and should look like this: Thank you for using the verifier,
    The Port25 Solutions, Inc. team
    ==========================================================
    Summary of Results
    ==========================================================
    SPF check: pass
    DomainKeys check: pass
    DKIM check: pass
    Sender-ID check: pass
    SpamAssassin check: ham
    ==========================================================
  • cd /etc/init.d/; chkconfig --add sender-id

    this adds the script to the list of programs which should run after restart. Do this for each init script

Last thoughts

I invite any reader to help improving this newbie-guide based on their experience and knowledge. There is plenty room for suggestions and additions to this article but this is an area where with all documentation available it seems that it is not enough or not covers all configuration or problems which may occur when setting up such a system. I spent myself several (many) good hours reading and learning, trying and failing or succeeding and finally writing this article hoping that will help others to save some of their time and neurons while trying to follow the latest recommendation of the war against spammers.

Although not required, a donation is always welcome end encouraging ;)

Donation



using PayPal

Resources

Files

DKIM configuration file /etc/dkim.conf

DKIM startup script /etc/init.d/dkim

Domainkeys startup script /etc/init.d/domainkeys

Sender-id startup script /etc/init.d/sender-id

Links

How to Setup DomainKeys Identified Mail (DKIM) with Postfix and Ubuntu Server

http://www.unibia.com/unibianet/systems-networking/how-setup-domainkeys-identified-mail-dkim-postfix-and-ubuntu-server

Setting up DKIM, SPF, Domainkeys DNS, Regular DNS on CentOS 5.3 at Pacificrack.com

http://palma-seo.com/setting-dkim-spf-domainkeys-dns-bind

Set Up DKIM On Postfix With dkim-milter (CentOS 5.2)

http://www.howtoforge.com/set-up-dkim-on-postfix-with-dkim-milter-centos-5.2

DomainKey Implementor's Tools and Library for email servers & clients

http://domainkeys.sourceforge.net/

Set Up DKIM For Multiple Domains On Postfix With dkim-milter 2.8.x (CentOS 5.3)

http://www.howtoforge.com/set-up-dkim-for-multiple-domains-on-postfix-with-dkim-milter-2.8.x-centos-5.3

Setting Up SPF, SenderId, Domain Keys, and DKIM

http://www.digitalsanctuary.com/tech-blog/debian/setting-up-spf-senderid-domain-keys-and-dkim.html

Domain Key / DKIM Key Generation Wizard

http://www.socketlabs.com/services/dkwiz

Domainkeys and DKIM with Postfix – Gentoo Linux wiki

http://en.gentoo-wiki.com/wiki/Domainkeys_and_DKIM_with_Postfix

Postfix Add-on Software

http://www.postfix.org/addon.html

DomainKeys Identified Mail (DKIM) Signatures – RFC

http://www.ietf.org/rfc/rfc4871.txt

Postfix / DKIM

https://help.ubuntu.com/community/Postfix/DKIM

How To Implement SPF In Postfix

http://www.howtoforge.com/postfix_spf

Email authentication guide made by BITS

http://www.bits.org/downloads/Publications%20Page/BITSSenderAuthenticationDeploymentFINALJun09.pdf

Return Path’s Best Practice Guide

http://www.returnpath.net/downloads/resources/Email%20Authentication%20v080107.pdf

Last update: May 5th, 2010


RegEx email parser

Posted: April 16th, 2008 | Author: | Filed under: Developers | No Comments »

Split email formats in pieces through preg_match:

$str=' (|(([^a-zA-Z0-9_\-.]|)(.*?)(|[^a-zA-Z0-9_\-.]+?)([a-zA-Z0-9_\-.]+?)(|[^a-zA-Z0-9_\-.]+?)))(|[^a-zA-Z0-9_\-.])([a-zA-Z0-9_\-.]+?)(|[^a-zA-Z0-9_\-.])@(.+?)\.([^>]+)';


Email validity check

Posted: April 16th, 2008 | Author: | Filed under: Developers | No Comments »

PHP code:


<pre lang="php">
<?php 
function is_email($email) {
  $x = '\d\w!\#\$%&\'*+\-/=?\^_`{|}~';    //just for clarity
  return   count($email = explode('@', $email, 3)) == 2 && 
      strlen($email[0]) < 65 && strlen($email[1]) < 256 && 
      preg_match("#^[$x]+(\.?([$x]+\.)*[$x]+)?$#", $email[0]) && 
      preg_match('#^(([a-z0-9]+-*)?[a-z0-9]+\.)+[a-z]{2,6}.?$#', $email[1]);
}
?>
</pre>


Contact us

Posted: March 22nd, 2008 | Author: | Filed under: General | No Comments »

%%wpcontactform%%