12 - Postfix com SMTP-AUTH And TLS

Agora, instalaremos o Postfix e o Dovecot (Dovecot será nosso servidor POP3/IMAP):

yum install cyrus-sasl cyrus-sasl-devel cyrus-sasl-gssapi cyrus-sasl-md5 cyrus-sasl-plain postfix dovecot

Agora, configuraremos o SMTP-AUTH e o TLS:

postconf -e ’smtpd_sasl_local_domain =’
postconf -e ’smtpd_sasl_auth_enable = yes’
postconf -e ’smtpd_sasl_security_options = noanonymous’
postconf -e ‘broken_sasl_auth_clients = yes’
postconf -e ’smtpd_recipient_restrictions = permit_sasl_authenticated,permit_mynetworks,reject_unauth_destination’
postconf -e ‘inet_interfaces = all’
postconf -e ‘mynetworks = 127.0.0.0/8′

Devemos editar o arquivo /usr/lib/sasl2/smtpd.conf de forma que o Postfix permita logins PLAIN e LOGIN. Ele deve se parecer com isto:

vi /usr/lib/sasl2/smtpd.conf

pwcheck_method: saslauthd
mech_list: plain login

Logo após, criamos os certificados para o TLS:

mkdir /etc/postfix/ssl
cd /etc/postfix/ssl/
openssl genrsa -des3 -rand /etc/hosts -out smtpd.key 1024

chmod 600 smtpd.key
openssl req -new -key smtpd.key -out smtpd.csr

openssl x509 -req -days 3650 -in smtpd.csr -signkey smtpd.key -out smtpd.crt

openssl rsa -in smtpd.key -out smtpd.key.unencrypted

mv -f smtpd.key.unencrypted smtpd.key
openssl req -new -x509 -extensions v3_ca -keyout cakey.pem -out cacert.pem -days 3650

A seguir, configuramos o Postfix para o TLS:

postconf -e ’smtpd_tls_auth_only = no’
postconf -e ’smtp_use_tls = yes’
postconf -e ’smtpd_use_tls = yes’
postconf -e ’smtp_tls_note_starttls_offer = yes’
postconf -e ’smtpd_tls_key_file = /etc/postfix/ssl/smtpd.key’
postconf -e ’smtpd_tls_cert_file = /etc/postfix/ssl/smtpd.crt’
postconf -e ’smtpd_tls_CAfile = /etc/postfix/ssl/cacert.pem’
postconf -e ’smtpd_tls_loglevel = 1′
postconf -e ’smtpd_tls_received_header = yes’
postconf -e ’smtpd_tls_session_cache_timeout = 3600s’
postconf -e ‘tls_random_source = dev:/dev/urandom’
postconf -e ’smtpd_sasl_authenticated_header = yes’

Então, configuramos o hostname em sua instalação de Postfix (tenha certeza que você modificou server1.example.com para o seu próprio hostname):

postconf -e ‘myhostname = server1.example.com’

Depois destes passos de configuração, você deveria ter um arquivo /etc/postfix/main.cf que se parece com este (Removi todos os comentários dele):

cat /etc/postfix/main.cf

queue_directory = /var/spool/postfix
command_directory = /usr/sbin
daemon_directory = /usr/libexec/postfix
mail_owner = postfix
inet_interfaces = all
mydestination = $myhostname, localhost.$mydomain, localhost
unknown_local_recipient_reject_code = 550
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
debug_peer_level = 2
debugger_command =
PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin
xxgdb $daemon_directory/$process_name $process_id & sleep 5

sendmail_path = /usr/sbin/sendmail.postfix
newaliases_path = /usr/bin/newaliases.postfix
mailq_path = /usr/bin/mailq.postfix
setgid_group = postdrop
html_directory = no
manpage_directory = /usr/share/man
sample_directory = /usr/share/doc/postfix-2.4.5/samples
readme_directory = /usr/share/doc/postfix-2.4.5/README_FILES
smtpd_sasl_local_domain =
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
broken_sasl_auth_clients = yes
smtpd_recipient_restrictions = permit_sasl_authenticated,permit_mynetworks,reject_unauth_destination
mynetworks = 127.0.0.0/8
smtpd_tls_auth_only = no
smtp_use_tls = yes
smtpd_use_tls = yes
smtp_tls_note_starttls_offer = yes
smtpd_tls_key_file = /etc/postfix/ssl/smtpd.key
smtpd_tls_cert_file = /etc/postfix/ssl/smtpd.crt
smtpd_tls_CAfile = /etc/postfix/ssl/cacert.pem
smtpd_tls_loglevel = 1
smtpd_tls_received_header = yes
smtpd_tls_session_cache_timeout = 3600s
tls_random_source = dev:/dev/urandom
smtpd_sasl_authenticated_header = yes
myhostname = server1.example.com

Agora, inicialize o Postfix, o saslauthd e o Dovecot:

chkconfig –levels 235 sendmail off
chkconfig –levels 235 postfix on
chkconfig –levels 235 saslauthd on
chkconfig –levels 235 dovecot on
/etc/init.d/sendmail stop
/etc/init.d/postfix start
/etc/init.d/saslauthd start
/etc/init.d/dovecot start

Para verificar se o SMTP-AUTH e o TLS funcionam normalmente, execute agora o seguinte comando:

telnet localhost 25

Depois que você estabeleceu a conexão com o seu servidor de e-mail Postfix, digite

ehlo localhost

Se você vir as linhas

250-STARTTLS

e

250-AUTH LOGIN PLAIN

tudo está bem.

[root@server1 ssl]# telnet localhost 25
Trying 127.0.0.1…
Connected to localhost.
Escape character is ‘^]’.
220 server1.example.com ESMTP Postfix
ehlo localhost
250-server1.example.com
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-STARTTLS
250-AUTH LOGIN PLAIN
250-AUTH=LOGIN PLAIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
quit
221 2.0.0 Bye
Connection closed by foreign host.
[root@server1 ssl]#

Digite

quit

para retornar ao shell do sistema.

12.1 - Maildir

O Dovecot utiliza o formato Maildir (e não o mbox). Então, se você instalar o ISPConfig no servidor, por favor, certifique-se de que você habilitou o Maildir em Management -> Server -> Settings -> Email. O ISPConfig irá, então, fazer a configuração necessária.

Se você não quiser instalar o ISPConfig, então você deve configurar o Postfix para entregar e-mails para o Maildir de um usuário(Você também pode fazer isto se você utilizar o ISPConfig - não vai doer ;-) ):

postconf -e ‘home_mailbox = Maildir/’
postconf -e ‘mailbox_command =’
/etc/init.d/postfix restart

13 - Apache2 com PHP5

Agora, instalaremos o Apache com o PHP5 (isto é, o PHP 5.2.4):

yum install php php-devel php-gd php-imap php-ldap php-mysql php-odbc php-pear php-xml php-xmlrpc curl curl-devel perl-libwww-perl ImageMagick libxml2 libxml2-devel

Então, edite o arquivo /etc/httpd/conf/httpd.conf:

vi /etc/httpd/conf/httpd.conf

e mude a entrada DirectoryIndex para

[...]
DirectoryIndex index.html index.htm index.shtml index.cgi index.php index.php3 index.pl
[...]

Agora, configure o seu sistema para inicializar o Apache na inicialização:

chkconfig –levels 235 httpd on

Inicialize o Apache:

/etc/init.d/httpd start

13.1 - Desabilitar o PHP Globalmente

(Se você não planeja instalar o ISPConfig neste servidor, por favor, pule esta seção!)

No ISPConfig, você irá configurar o PHP em uma base por-website, ex: você poderá especificar que website poderá rodar scripts PHP e qual não poderá. Isto pode funcionar somente se o PHP estiver deabilitado globalmente, porque, de outra forma, todos os websites poderiam executar scripts PHP, não importa o que você especificar no ISPConfig.

Para desabilitar o PHP globalmente, editamos o arquivo /etc/httpd/conf.d/php.conf e comente as linhas AddHandler e AddType:

vi /etc/httpd/conf.d/php.conf

#
# PHP is an HTML-embedded scripting language which attempts to make it
# easy for developers to write dynamically generated webpages.
#

LoadModule php5_module modules/libphp5.so

#
# Cause the PHP interpreter to handle files with a .php extension.
#
#AddHandler php5-script .php
#AddType text/html .php

#
# Add index.php to the list of files that will be served as directory
# indexes.
#
DirectoryIndex index.php

#
# Uncomment the following line to allow PHP to pretty-print .phps
# files as PHP source code:
#
#AddType application/x-httpd-php-source .phps

Logo após, reinicializamos o Apache:

/etc/init.d/httpd restart
Continua…