Product SiteDocumentation Site

فصل 11. خدمت‌های شبکه‌ای: پست‌فیکس، آپاچی، ان‌اف‌اس، سامبا، اسکویید، ال‌دپ، سیپ، اکس‌ام‌پی‌پی، ترن

11.1. کارساز نامه
11.1.1. نصب کردن پست‌فیکس
11.1.2. پیکربندی دامنه‌های مجازی
11.1.3. محدودیت‌های گرفتن و فرستادن
11.1.4. برپایی فهرست خاکستری
11.1.5. سفارشی‌سازی فیلترها مبتنی بر گیرنده
11.1.6. Integrating an Antivirus Filter
11.1.7. Fighting Spam with SPF, DKIM and DMARC
11.1.8. SMTP احرازهویت شده
11.2. سرور وب (HTTP)
11.2.1. نصب آپاچی
11.2.2. Adding support for SSL
11.2.3. پیکربندی گروه‌های مجازی
11.2.4. عبارت‌های متداول
11.2.5. تحلیلگرهای گزارش
11.3. سرور فایل FTP
11.4. سرور فایل NFS
11.4.1. ایمن‌سازی NFS
11.4.2. سرور NFS
11.4.3. برنامه NFS
11.5. راه‌اندازی اشتراک‌های ویندوز با Samba
11.5.1. سرور سامبا
11.5.2. برنامه سامبا
11.6. پروکسی HTTP/FTP
11.6.1. نصب
11.6.2. پیکربندی یک حافظه نهان
11.6.3. پیکربندی یک فیلتر
11.7. دایرکتوی LDAP
11.7.1. نصب
11.7.2. پرکردن دایرکتوری
11.7.3. مدیریت حساب‌ها با LDAP
11.8. سرویس‌های ارتباطی بلادرنگ
11.8.1. تنظیمات DNS برای سرویس‌های RTC
11.8.2. سرور TURN
11.8.3. سرور پروکسی SIP
11.8.4. سرور XMPP
11.8.5. اجرای سرویس‌ها روی درگاه ۴۴۳
11.8.6. افزودن WebRTC
خدمت‌های شبکه‌ای برنامه‌هاییند که کاربران در کارهای روزانه‌شان مستقیماً با آن‌ها سر و کار دارند. آن‌ها نوک کوه یخ سامانه‌های اطّلاعاتی هستند و این فصل روی آن‌ها متمرکز است. بخش نهفته‌ای که به آن وابسته‌اند زیرساخت‌هاییست که پیش‌تر شرح دادیم. معمولاً این خدمت‌ها نیازمند فناوری رمزنگاری‌ای هستند که در قسمت 10.2, “X.509 certificates” توضیح داده شده.

11.1. کارساز نامه

The Falcot Corp administrators selected Postfix for the electronic mail server, due to its reliability and its ease of configuration. Indeed, its design enforces that each task is implemented in a process with the minimum set of required permissions, which is a great mitigation measure against security problems.

11.1.1. نصب کردن پست‌فیکس

The postfix package includes the main SMTP daemon. Other packages (such as postfix-ldap and postfix-pgsql) add extra functionality to Postfix, including access to mapping databases. You should only install them if you know that you need them.
هنگام نصب بسته چندین پرسش Debconf مطرح می‌شود. پاسخ‌ها نخستین نگارش پروندهٔ پیکربندی /etc/postfix/main.cf را ایجاد می‌کند.
The first question deals with the type of setup. Only two of the proposed answers are relevant in case of an Internet-connected server, “Internet site” and “Internet with smarthost”. The former is appropriate for a server that receives incoming email and sends outgoing email directly to its recipients, and is therefore well-adapted to the Falcot Corp case. The latter is appropriate for a server receiving incoming email normally, but that sends outgoing email through an intermediate SMTP server — the “smarthost” — rather than directly to the recipient's server. This is mostly useful for individuals with a dynamic IP address, since many email servers reject messages coming straight from such an IP address. In this case, the smarthost will usually be the ISP's SMTP server, which is always configured to accept email coming from the ISP's customers and forward it appropriately. This setup (with a smarthost) is also relevant for servers that are not permanently connected to the internet, since it avoids having to manage a queue of undeliverable messages that need to be retried later.
The second question deals with the full name of the machine, used to generate email addresses from a local user name; the full name of the machine ends up as the part after the at-sign (“@”). In the case of Falcot, the answer should be mail.falcot.com. This is the only question asked by default, but the configuration it leads to is not complete enough for the needs of Falcot, which is why the administrators run dpkg-reconfigure postfix so as to be able to customize more parameters.
One of the extra questions asks for all the domain names related to this machine. The default list includes its full name as well as a few synonyms for localhost, but the main falcot.com domain needs to be added by hand. More generally, this question should usually be answered with all the domain names for which this machine should serve as an MX server; in other words, all the domain names for which the DNS says that this machine will accept email. This information ends up in the mydestination variable of the main Postfix configuration file — /etc/postfix/main.cf.
نقش رکورد MX ساناد هنگام فرستادن رایانامه

شكل 11.1. نقش رکورد MX ساناد هنگام فرستادن رایانامه

In some cases, the installation can also ask what networks should be allowed to send email via the machine. In its default configuration, Postfix only accepts emails coming from the machine itself; the local network will usually be added. The Falcot Corp administrators added 192.168.0.0/16 to the default answer. If the question is not asked, the relevant variable in the configuration file is mynetworks, as seen in the example below.
Local email can also be delivered through procmail. This tool allows users to sort their incoming email according to rules stored in their ~/.procmailrc file. Both Postfix and Exim4 suggest procmail by default, but there are alternatives like maildrop or Sieve filters.
After this first step, the administrators got the following configuration file; it will be used as a starting point for adding some extra functionality in the next sections.

مثال 11.1. پروندهٔ نخستین /etc/postfix/main.cf

# See /usr/share/postfix/main.cf.dist for a commented, more complete version


# Debian specific:  Specifying a file name will cause the first
# line of that file to be used as the name.  The Debian default
# is /etc/mailname.
#myorigin = /etc/mailname

smtpd_banner = $myhostname ESMTP $mail_name (Debian/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

readme_directory = no

# See http://www.postfix.org/COMPATIBILITY_README.html -- default to 2 on
# fresh installs.
compatibility_level = 2



# TLS parameters
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_tls_security_level=may

smtp_tls_CApath=/etc/ssl/certs
smtp_tls_security_level=may
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache


smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination
myhostname = mail.falcot.com
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
mydestination = mail.falcot.com, falcot.com, localhost.localdomain, localhost
relayhost = 
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128 192.168.0.0/16
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all
default_transport = smtp
relay_transport = smtp
inet_protocols = all
myorigin = /etc/mailname

11.1.2. پیکربندی دامنه‌های مجازی

The mail server can receive mails addressed to other domains besides the main domain; these are then known as “virtual“ domains. In most cases where this happens, the emails are not ultimately destined to local users. Postfix provides two interesting features for handling virtual domains.

11.1.2.1. دامنه‌های دگرنام مجازی

دامنهٔ دگرنام مجازی تنها دگرنام‌هاست: نشانی‌هایی که تنها رایانامه‌ها را به دیگر نشانی‌ها هدایت می‌کنند.
چنین دامنه‌ای با افزودن نامش به متغیّر virtual_alias_domains و ارجاع دادن به یک پروندهٔ نگاشت نشانی در متغیّر virtual_alias_maps، به کار می‌افتد.
virtual_alias_domains = falcotsbrand.com
virtual_alias_maps = hash:/etc/postfix/virtual
The /etc/postfix/virtual file describes a mapping with a rather straightforward syntax: each line contains two fields separated by whitespace; the first field is the alias name, the second field is a list of email addresses where it redirects. The special @domain.com syntax covers all remaining aliases in a domain.
webmaster@falcotsbrand.com  jean@falcot.com
contact@falcotsbrand.com    laure@falcot.com, sophie@falcot.com
# The alias below is generic and covers all addresses within
# the falcotsbrand.com domain not otherwise covered by this file.
# These addresses forward email to the same user name in the
# falcot.com domain.
@falcotsbrand.com           @falcot.com
پس از تغییر دادن etc/postfix/virtual باید جدول /etc/postfix/virtual.db پست‌فیکس با استفاده از sudo postmap /etc/postfix/virtual به‌روز شود.

11.1.2.2. دامنه‌های صندوق‌پستی مجازی

Messages addressed to a virtual mailbox domain are stored in mailboxes not assigned to a local system user.
Enabling a virtual mailbox domain requires naming this domain in the virtual_mailbox_domains variable, and referencing a mailbox mapping file in virtual_mailbox_maps. The virtual_mailbox_base parameter contains the directory under which the mailboxes will be stored.
virtual_mailbox_domains = falcot.org
virtual_mailbox_maps = hash:/etc/postfix/vmailbox
virtual_mailbox_base = /var/mail/vhosts
The virtual_uid_maps parameter (respectively virtual_gid_maps) references the file containing the mapping between the email address and the system user (respectively group) that “owns” the corresponding mailbox. To get all mailboxes owned by the same owner/group, the static:5000 syntax assigns a fixed UID/GID (of value 5000 here).
Again, the syntax of the /etc/postfix/vmailbox file is quite straightforward: two fields separated with whitespace. The first field is an email address within one of the virtual domains, and the second field is the location of the associated mailbox (relative to the directory specified in virtual_mailbox_base). If the mailbox name ends with a slash (/), the emails will be stored in the maildir format; otherwise, the traditional mbox format will be used. The maildir format uses a whole directory to store a mailbox, each individual message being stored in a separate file. In the mbox format, on the other hand, the whole mailbox is stored in one file, and each line starting with “From ” (From followed by a space) signals the start of a new message.
# Jean's email is stored as maildir, with
# one file per email in a dedicated directory
jean@falcot.org falcot.org/jean/
# Sophie's email is stored in a traditional "mbox" file,
# with all mails concatenated into one single file
sophie@falcot.org falcot.org/sophie

11.1.3. محدودیت‌های گرفتن و فرستادن

The growing number of unsolicited bulk emails (spam) requires being increasingly strict when deciding which emails a server should accept. This section presents some of the strategies included in Postfix.
If the reject-rules are too strict, it may happen that even legitimate email traffic gets locked out. It is therefor a good habit to test restrictions and prevent the permanent rejection of requests during this time using the soft_bounce = yes directive. By prepending a reject-type directive with warn_if_reject only a log message will be recorded instead of rejecting the request.

11.1.3.1. محدودیت‌های دسترسی مبتنی بر IP

The smtpd_client_restrictions directive controls which machines are allowed to communicate with the email server.
When a variable contains a list of rules, as in the example below, these rules are evaluated in order, from the first to the last. Each rule can accept the message, reject it, or leave the decision to a following rule. As a consequence, order matters, and simply switching two rules can lead to a widely different behavior.

مثال 11.2. محدودیت‌های مبتنی بر نشانی کارخواه

smtpd_client_restrictions =
    permit_mynetworks,
    warn_if_reject reject_unknown_client_hostname,
    check_client_access hash:/etc/postfix/access_clientip,
    reject_rhsbl_reverse_client dbl.spamhaus.org,
    reject_rhsbl_reverse_client rhsbl.sorbs.net,
    reject_rbl_client zen.spamhaus.org,
    reject_rbl_client dnsbl.sorbs.net
The permit_mynetworks directive, used as the first rule, accepts all emails coming from a machine in the local network (as defined by the mynetworks configuration variable).
The second directive would normally reject emails coming from machines without a completely valid DNS configuration. Such a valid configuration means that the IP address can be resolved to a name, and that this name, in turn, resolves to the IP address. This restriction is often too strict, since many email servers do not have a reverse DNS for their IP address. This explains why the Falcot administrators prepended the warn_if_reject modifier to the reject_unknown_client directive: this modifier turns the rejection into a simple warning recorded in the logs. The administrators can then keep an eye on the number of messages that would be rejected if the rule were actually enforced, and make an informed decision later if they wish to enable such enforcement.
The check_client_access directive allows the administrator to set up a blacklist and a whitelist of email servers, stored in the /etc/postfix/access_clientip file. Servers in the whitelist are considered as trusted, and the emails coming from there therefore do not go through the following filtering rules.
The last four rules reject any message coming from a server listed in one of the indicated blacklists. RBL is an acronym for Remote Black List, and RHSBL stands for Right-Hand Side Black List. The difference is that the former lists IP addresses, whereas the latter lists domain names. There are several such services. They list domains and IP addresses with poor reputation, badly configured servers that spammers use to relay their emails, as well as unexpected mail relays such as machines infected with worms or viruses.

11.1.3.2. بررسی کردن اعتبار دستورات EHLO یا HELO

Each SMTP exchange starts with a HELO (or EHLO) command, followed by the name of the sending email server. Checking the validity of this name can be interesting. To fully enforce the restrictions listed in smtpd_helo_restrictions the smtpd_helo_required option needs to be enabled. Otherwise clients could skip the restrictions by not sending any HELO/EHLO command.

مثال 11.3. محدودیت‌های نام اعلام شده در EHLO

smtpd_helo_required = yes
smtpd_helo_restrictions =
    permit_mynetworks,
    reject_invalid_helo_hostname,
    reject_non_fqdn_helo_hostname,
    warn_if_reject reject_unknown_helo_hostname,
    check_helo_access hash:/etc/postfix/access_helo,
    reject_rhsbl_helo multi.surbl.org
عبارت ابتدایی permit_mynetworks به تمام رایانه‌های شبکه محلی اجازه می‌دهد که خود را آزادانه معرفی کنند. این مهم است چرا که برخی برنامه‌های ایمیل برای این بخش از پروتکل SMTP به اندازه کافی احترام قائل نیستند و آن‌ها می‌توانند خود را با نام‌های مهمل معرفی کنند.
The reject_invalid_helo_hostname rule rejects emails when the EHLO announce lists a syntactically incorrect hostname. The reject_non_fqdn_helo_hostname rule rejects messages when the announced hostname is not a fully-qualified domain name (including a domain name as well as a host name). The reject_unknown_helo_hostname rule rejects messages if the announced name does not exist in the DNS. Since this last rule unfortunately leads to a lot of rejections, the administrators turned its effect to a simple warning with the warn_if_reject modifier as a first step; they may decide to remove this modifier at a later stage, after auditing the results of this rule.
The reject_rhsbl_helo allows one to specify a black list to check the hostname against an RHSBL.
Using permit_mynetworks as the first rule has an interesting side effect: the following rules only apply to hosts outside the local network. This allows blacklisting all hosts that announce themselves as part of the falcot.com network, for instance by adding a falcot.com REJECT You are not in our network! line to the /etc/postfix/access_helo file.

11.1.3.3. Accepting or Refusing Mails Based on the Announced Sender

هر پیام شامل فرستنده است، که توسط دستور MAIL FROM از پروتکل SMTP اعلام می‌شود؛ مجدد، این اطلاعات می‌توانند به چندین روش ارزیابی گردند.

مثال 11.4. بررسی‌های فرستنده

smtpd_sender_restrictions =
    check_sender_access hash:/etc/postfix/access_sender,
    reject_unknown_sender_domain,
    reject_unlisted_sender,
    reject_non_fqdn_sender,
    reject_rhsbl_sender rhsbl.sorbs.net
جدول /etc/postfix/access_sender با برخی فرستندگان به شیوه‌ای خاص برخورد می‌کند. این معمولا به معنی قراردادن برخی فرستندگان در فهرست سیاه و سفید است.
The reject_unknown_sender_domain rule requires a valid sender domain, since it is needed for a valid address. The reject_unlisted_sender rule rejects local senders if the address does not exist; this prevents emails being sent from an invalid address in the falcot.com domain, and messages emanating from joe.bloggs@falcot.com are only accepted if such an address really exists.
در نهایت، قانون reject_non_fqdn_sender پیام‌هایی که از یک نشانی بدون نام دامنه تمام-عیار ارسال شوند را رد می‌کند. در عمل، یعنی پیام‌هایی که به صورت user@machine باشند: نشانی باید به صورت user@machine.example.com یا user@example.com اعلام گردد.
The reject_rhsbl_sender rule reject senders based on a (domain-based) RHSBL service.

11.1.3.4. Accepting or Refusing Mails Based on the Recipient

هر ایمیل حداقل یک گیرنده دارد، که توسط دستور RCPT TO از پروتکل SMTP معرفی می‌شود. این نشانی‌ها همچنین عملیات اعتبارسنجی را تضمین می‌کنند، حتی اگر از بررسی‌های انجام شده روی نشانی فرستنده مشخص نباشد.

مثال 11.5. بررسی‌های گیرنده

smtpd_recipient_restrictions =
    permit_mynetworks,
    reject_unauth_destination,
    reject_unlisted_recipient,
    reject_non_fqdn_recipient,
    permit
reject_unauth_destination قانون پایه‌ای است که پیام‌های خارجی را ملزم می‌سازد نشانی ما را داشته باشند؛ پیام‌هایی که به نشانی خارج از محدوده پوشش این سرور فرستاده شوند رد می‌شوند. بدون این قانون، یک سرور به سیستم بازی تبدیل می‌شود که به اسپمرها اجازه می‌دهد پیام‌های ناخواسته خود را ارسال کنند؛ بنابراین این قانون الزامی است و بهتر است که در قسمت ابتدایی فهرست قرار گیرد، تا قبل از مشخص نشدن گیرنده یک پیام، هیچ قانون دیگری نتواند آن را بررسی کند.
قانون reject_unlisted_recipient پیام‌های ارسالی به کاربران غیرواقعی را رد می‌کند، که با عقل جور در می‌آید. در نهایت، قانون reject_non_fqdn_recipient نشانی‌هایی که تمام-عیار نباشند را رد می‌کند؛ این قانون منجر می‌شود که پیام به jean یا jean@machine ارسال نشود و نیازمند نشانی کامل می‌باشد از جمله jean@machine.falcot.com یا jean@falcot.com.
The permit directive at the end is not necessary. But it can be useful at the end of a restriction list to make the default policy explicit.

11.1.3.5. محدودیت‌های مرتبط با دستور DATA

دستور DATA از پروتکل SMTP قبل از محتوای پیام قرار می‌گیرد. با اینکه هیچ اطلاعات خاصی را فراهم نمی‌کند، بجز معرفی قسمت بعدی، می‌تواند مورد بررسی قرار گیرد.

مثال 11.6. بررسی‌های DATA

smtpd_data_restrictions = reject_unauth_pipelining
عبارت reject_unauth_pipelining زمانی منجر به رد پیام می‌شود که فرستنده آن دستوری را قبل از پاسخ به دستور قبلی فرستاده باشد. این عملکرد در مقابل شیوه کار ربات‌های اسپمر قرار می‌گیرد، چرا که آن‌ها اغلب اهمیتی به پاسخ‌ها نمی‌دهند و تمرکز خود را روی ارسال بیشترین پیام در کمترین زمان معطوف می‌سازند.

11.1.3.6. اعمال محدودیت‌ها

Although the above commands validate information at various stages of the SMTP exchange, Postfix sends the actual rejection as a reply to the RCPT TO command by default.
یعنی حتی اگر پیام مبتنی بر یک دستور نامعتبر ... رد شده باشد، Postfix از فرستنده و گیرنده پیام هنگام اعلام رد آن آگاه است. در این مرحله است که می‌تواند پیام مرتبط‌تری را در فایل‌های گزارش قرار دهد تا اینکه همان ابتدای کار این فرآیند متوقف می‌شد. علاوه بر این، تعدادی از برنامه‌های SMTP انتظار شکست عملیات در همان دستورات ابتدایی SMTP را ندارند و این برنامه‌ها کمتر با این عملیات رد دیرموقع، مختل می‌‌شوند.
مزیت نهایی این انتخاب این است که قوانین می‌توانند طی مراحل مختلف تبادل SMTP اطلاعات زیادی کسب کنند؛ این باعث می‌شود مجوزهای بهتری را بتوان تعریف کرد، از جمله رد یک ارتباط غیر-محلی که خود را به عنوان یک فرستنده محلی معرفی کرده باشد.
The default behavior is controlled by the smtpd_delay_reject rule.

11.1.3.7. فیلترکردن مبتنی بر محتوای پیام

سیستم ارزیابی و ایجاد محدودیت بدون ایجاد روشی برای بررسی متن پیام کامل نیست. Postfix بین بررسی‌های انجام گرفته روی سرآیند ایمیل و بدنه آن تفاوت قائل می‌شود.

مثال 11.7. فعال‌سازی فیلترهای محتوا-محور

header_checks = regexp:/etc/postfix/header_checks
body_checks = regexp:/etc/postfix/body_checks
هر دو فایل شامل فهرستی از عبارت‌های منظم (که به نام regexps یا regexes نیز شاخته می‌شوند) و اقدامات مربوط به هر کدام در صورت تطبیق سرآیند یا بدنه ایمیل با آن‌ها می‌باشند.

مثال 11.8. نمونه فایل /etc/postfix/header_checks

/^X-Mailer: GOTO Sarbacane/ REJECT I fight spam (GOTO Sarbacane)
/^Subject: *Your email contains VIRUSES/ DISCARD virus notification
اولی به بررسی سرآیند می‌پردازد که آیا شامل نرم‌افزار ایمیل هست یا خیر؛ اگر GOTO Sarbacane (یک نرم‌افزار ایمیل انبوه) پیدا شد، پیام رد می‌شود. عبارت دوم نیز به بررسی محتوای پیام می‌پردازد؛ اگر شامل یک اطلاعیه ویروس باشد، می‌توان تصمیم گرفت بجای رد ایمیل، آن را نادیده بگیریم.
استفاده از این فیلترها مانند راه رفتن روی لبه تیغ است، چرا که در صورت عمومی در نظر گرفتن این قوانین ممکن است بسیاری از ایمیل‌های معتبر و قانونی را از دست بدهیم. در این موارد، نه تنها پیام‌ها از دست می‌روند، بلکه برای فرستندگان آنان نیز پیام‌های خطای آزاردهنده ارسال می‌گردد.

11.1.4. برپایی فهرست خاکستری

“Greylisting” is a filtering technique according to which a message is initially rejected with a temporary error code, and only accepted after a further delivery attempt with some delay. This filtering is particularly efficient against spam sent by the many machines infected by worms and viruses, since this software rarely acts as a full SMTP agent (by checking the error code and retrying failed messages later), especially since many of the harvested addresses are really invalid and retrying would only mean losing time.
Postfix به خودی خود شامل فهرست خاکستری نیست، اما گزینه‌ای وجود دارد که تصمیم‌گیری برای پذیرش یا رد یک پیام را می‌توان به یک برنامه خارجی سپرد. بسته postgrey شامل همین عملکرد است، که به منظور سیاست دسترسی به عنوان سرویس نماینده طراحی شده است.
زمانی که postgrey نصب شود، به عنوان یک فرآیند پس‌زمینه به درگاه ۱۰۰۲۳ گوش می‌کند. سپس می‌توان با تنظیم پارامتر check_policy_service در Postfix، آن را به این منظور پیکربندی کرد:
smtpd_recipient_restrictions =
    permit_mynetworks,
    [...]
    check_policy_service inet:127.0.0.1:10023
Each time Postfix reaches this rule in the rule set, it will connect to the postgrey daemon and send it information concerning the relevant message. On its side, Postgrey considers the IP address/sender/recipient triplet and checks in its database whether that same triplet has been seen recently. If so, Postgrey replies that the message should be accepted; if not, the reply indicates that the message should be temporarily rejected, and the triplet gets recorded in the database.
اشکال اصلی فهرست خاکستری این است که پیام‌های قانونی با تاخیر مواجه می‌شوند، که برای همیشه قابل قبول نیست. همچنین بار سرورهایی که بسیاری ایمیل‌های قانونی را ارسال می‌کنند، افزایش می‌‌دهد.

11.1.5. سفارشی‌سازی فیلترها مبتنی بر گیرنده

قسمت‌های قسمت 11.1.3, “محدودیت‌های گرفتن و فرستادن” و قسمت 11.1.4, “برپایی فهرست خاکستری بسیاری محدودیت‌های ممکن را بررسی کرده‌اند. همگی آن‌ها کاربرد خود برای محدودکردن بخشی از اسپم‌ها را دارند اما شامل نواقصی نیز هستند. بنابراین بسیار متداول است که مجموعه فیلترهای بکار رفته روی گیرنده را سفارشی‌سازی کرد. در شرکت فالکوت، استفاده از فهرست خاکستری در کار اغلب کاربران مشکلی بوجود نمی‌آورد اما برخی نیز نیاز به تاخیر بسیار کمی در کار خود دارند (از جمله واحد پشتیبانی فنی). به همین شکل، واحد تجاری گاهی اوقات مشکلاتی با برخی مشتریان آسیایی دارد که در فهرست سیاه قرار گرفته‌اند؛ این واحد درخواست یک نشانی بدون فیلتر به منظور مکاتبه با آن‌ها را دارد.
Postfix با استفاده از مفهوم “کلاس محدودیت” امکان سفارشی‌سازی فیلترها را فراهم می‌کند. کلاس‌ها در پارامتر smtpd_restriction_classes قرار گرفته و به شیوه مشابه smtpd_recipient_restrictions تعریف شده‌اند. عبارت check_recipient_access نگاشتی بین یک گیرنده و مجموعه‌ای از محدودیت‌های آن برقرار می‌کند.

مثال 11.9. تعریف کلاس‌های محدودیت در main.cf

smtpd_restriction_classes = greylisting, aggressive, permissive

greylisting = check_policy_service inet:127.0.0.1:10023
aggressive =
        reject_rbl_client sbl-xbl.spamhaus.org,
        check_policy_service inet:127.0.0.1:10023
permissive = permit

smtpd_recipient_restrictions =
        permit_mynetworks,
        reject_unauth_destination,
        check_recipient_access hash:/etc/postfix/recipient_access

مثال 11.10. فایل /etc/postfix/recipient_access

# Unfiltered addresses
postmaster@falcot.com  permissive
support@falcot.com     permissive
sales-asia@falcot.com  permissive

# Aggressive filtering for some privileged users
joe@falcot.com         aggressive

# Special rule for the mailing-list manager
sympa@falcot.com       reject_unverified_sender

# Greylisting by default
falcot.com             greylisting

11.1.6. Integrating an Antivirus Filter

The many viruses circulating as attachments to emails make it important to set up an antivirus solution at the entry point of the company network, since despite an awareness campaign, some users will still open attachments from obviously shady messages.
The Falcot administrators selected clamav from the homonymous package.
وظیفه برقراری ارتباط بین آنتی‌ویروس و سرور ایمیل بر عهده clamav-milter است. یک milter (که مخفف mail filter است) یک برنامه فیلترکردن بخصوص است که برای تعامل با سرورهای ایمیل طراحی شده است. یک milter از یک رابط استاندارد برنامه‌نویسی یا API استفاده می‌کند که عملکرد بهتری در مقایسه با فیلترهای خارجی برای سرورهای ایمیل فراهم می‌کند. milterها در ابتدا توسط Sendmail معرفی شدند اما Postfix به زودی از آن‌ها پشتیبانی کرد.
زمانی که بسته clamav-milter نصب شود، فیلتر ایمیل باید به گونه‌ای پیکربندی شود که روی یک درگاه TCP جدا از مقدار پیشفرض سوکت نامگذاری شده قرار بگیرد. اینکار با استفاده از dpkg-reconfigure clamav-milter صورت می‌گیرد. زمانی که پرسش “Communication interface with Sendmail” مطرح می‌شود، “inet:10002@127.0.0.1” را پاسخ دهید.
پیکربندی استاندارد ClamAV برای اکثر موقعیت‌های مناسب است، اما برخی پارامترهای مهم هنوز می‌توانند با استفاده از دستور dpkg-reconfigure clamav-base سفارشی گردند.
گام آخر اطلاع‌رسانی به Postfix درباره فیلتر تازه-پیکربندی شده است که به سادگی افزودن عبارت زیر به /etc/postfix/main.cf است:
# Virus check with clamav-milter
smtpd_milters = inet:[127.0.0.1]:10002
If the antivirus causes problems, this line can be commented out, and systemctl reload postfix should be run so that this change is taken into account.
اکنون تمام پیام‌های ارسالی به Postfix از طریق فیلتر آنتی‌ویروس عبور داده می‌شوند.

11.1.7. Fighting Spam with SPF, DKIM and DMARC

The high number of unsolicited email sent every day led to the creation of several standards, which aim at validating that the sending host of an email is authorized and that the email has not been tampered with. The following systems are all DNS-based and require the administrators to not only have control over the mail server, but over the DNS for the domain in question too.

11.1.7.1. Integrating the Sender Policy Framework (SPF)

The Sender Policy Framework (SPF) is used to validate if a certain mail server is allowed to send emails for a given domain. It is mostly configured through DNS. The syntax for the entry to make is explained in detail at:
The following is a sample DNS entry which states that all the domain's Mail Exchange Resource Records (MX-RRs) are allowed to send email for the current domain, and all others are prohibited. The DNS entry does not need to be given a name. But to use the include directive it must have one.
Name: example.org
Type: TXT
TTL:  3600
Data: v=spf1 a mx -all
Let's take a quick look at the falcot.org entry.
# host -t TXT falcot.org
falcot.org descriptive text "v=spf1 ip4:199.127.61.96 +a +mx +ip4:206.221.184.234 +ip4:209.222.96.251 ~all"
It states that the IP of the sender must match the A record for the sending domain, or must be listed as one of the Mail Exchange Resource Records for the current domain, or must be one of the three mentioned IP4 addresses. All other hosts should be marked as not being allowed to send email for the sender domain. The latter is called a "soft fail" and is intended to mark the email accordingly, but still accept it.
The postfix mail server can check the SPF record for incoming emails using the postfix-policyd-spf-python package, a policy agent written in Python. The file /usr/share/doc/postfix-policyd-spf-python/README.Debian describes the necessary steps to integrate the agent into postfix, so we won't repeat it here.
The configuration is done in the file /etc/postfix-policyd-spf-python/policyd-spf.conf, which is fully documented in policyd-spf.conf(5) and /usr/share/doc/postfix-policyd-spf-python/policyd-spf.conf.commented.gz. The main configuration parameters are HELO_reject and Mail_From_reject, which configure if emails should be rejected (Fail) or accepted with a header being appended (False), if checks fail. The latter is often useful, when the message is further processed by a spam filter.
If the result is intended to be used by opendmarc ( قسمت 11.1.7.3, “Integrating Domain-based Message Authentication, Reporting and Conformance (DMARC)” ), then Header_Type must be set to AR.
Note that spamassassin contains a plugin to check the SPF record.

11.1.7.2. Integrating DomainKeys (DKIM) Signing and Checking

The Domain Keys Identified Mail (DKIM) standard is a sender authentication system. The mail transport agent, here postfix, adds a digital signature associated with the domain name to the header of outgoing emails. The receiving party can validate the message body and header fields by checking the signature against a public key, which is retrieved from the senders DNS records.
The necessary tools are shipped with the opendkim and opendkim-tools packages.
First the private key must be created using the command opendkim-genkey -s SELECTOR -d DOMAIN. SELECTOR must be a unique name for the key. It can be as simple as "mail" or the date of creation, if you plan to rotate keys.

مثال 11.11. Create a private key for signing E-Mails from falcot.com

# opendkim-genkey -s mail -d falcot.com -D /etc/dkimkeys
# chown opendkim.opendkim /etc/dkimkeys/mail.*
This will create the files /etc/dkimkeys/mail.private and /etc/dkimkeys/mail.txt and set the appropriate ownership. The first file contains the private key, and the latter the public key that needs to be added to the DNS:
Name: mail._domainkey
Type: TXT
TTL:  3600
Data: "v=DKIM1; h=sha256; k=rsa; s=email; p=[...]"
The opendkim package in Debian defaults to a keysize of 2048 bit. Unfortunately, some DNS servers can only handle text entries with a maximum length of 255 characters, which is exceeded by the chosen default keysize. In this case use the option -b 1024 to chose a smaller keysize. If opendkim-testkey succeeds, the entry has been successfully set up. The syntax of the entry is explained here:
To configure opendkim, SOCKET and RUNDIR must be chosen in /etc/default/opendkim. Please note that SOCKET must be accessible from postfix in its chrooted environment. The further configuration is done in /etc/opendkim.conf. The following is a configuration excerpt, which makes sure that the Domain "falcot.com" and all subdomains (SubDomain) are signed by the Selector "mail" and the single private key (KeyFile) /etc/dkimkeys/mail.private. The "relaxed" Canonicalization for both the header and the body tolerates mild modification (by a mailing list software, for example). The filter runs both in signing ("s") and verification ("v") Mode. If a signature fails to validate (On-BadSignature), the mail should be quarantined ("q").
[...]
Domain                  falcot.com
KeyFile                 /etc/dkimkeys/mail.private
Selector                mail

[...]
Canonicalization        relaxed/relaxed
Mode                    sv
On-BadSignature         q
SubDomains              yes

[...]
Socket                  inet:12345@localhost

[...]
UserID                  opendkim
It is also possible to use multiple selectors/keys (KeyTable), domains (SigningTable) and to specify internal or trusted hosts (InternalHosts, ExternalIgnoreList), which may send mail through the server as one of the signing domains without credentials.
The following directives in /etc/postfix/main.cf make postfix use the filter:
milter_default_action = accept
non_smtpd_milters = inet:localhost:12345
smtpd_milters = inet:localhost:12345
To differentiate signing and verification it is sometimes more useful to add the directives to the services in /etc/postfix/master.cf instead.
More information is available in the /usr/share/doc/opendkim/ directory and the manual pages opendkim(8) and opendkim.conf(5).
Note that spamassassin contains a plugin to check the DKIM record.

11.1.7.3. Integrating Domain-based Message Authentication, Reporting and Conformance (DMARC)

The Domain-based Message Authentication, Reporting and Conformance (DMARC) standard can be used to define a DNS TXT entry with the name _dmarc and the action that should be taken when emails that contain your domain as the sending host fail to validate using DKIM and SPF.
Let's have a look at the entries of two large providers:
# host -t TXT _dmarc.gmail.com
_dmarc.gmail.com descriptive text "v=DMARC1; p=none; sp=quarantine; rua=mailto:mailauth-reports@google.com"
# host -t TXT _dmarc.yahoo.com
_dmarc.yahoo.com descriptive text "v=DMARC1; p=reject; pct=100; rua=mailto:d@rua.agari.com; ruf=mailto:d@ruf.agari.com;"
Yahoo has a strict policy to reject all emails pretending to be sent from a Yahoo account but missing or failing DKIM and SPF checks. Google Mail (Gmail) propagates a very relaxed policy, in which such messages from the main domain should still be accepted (p=none). For subdomains they should be marked as spam (sp=quarantine). The addresses given in the rua key can be used to send aggregated DMARC reports to. The full syntax is explained here:
The postfix mail server can use this information too. The opendmarc package contains the necessary milter. Similar to opendkim SOCKET and RUNDIR must be chosen in /etc/default/opendmarc (for Unix sockets you must make sure that they are inside the postfix chroot to be found). The configuration file /etc/opendmarc.conf contains detailed comments and is also explained in opendmarc.conf(5). By default, emails failing the DMARC validation are not rejected but flagged, by adding an appropriate header field. To change this, use RejectFailures true.
The milter is then added to smtpd_milters and non_smtpd_milters. If we configured the opendkim and opendmarc milters to run on ports 12345 and 54321, the entry in /etc/postfix/main.cf looks like this:
non_smtpd_milters = inet:localhost:12345,inet:localhost:54321
smtpd_milters = inet:localhost:12345,inet:localhost:54321
The milter can also be selectively applied to a service in /etc/postfix/master.cf instead.

11.1.8. SMTP احرازهویت شده

به منظور امکان ارسال ایمیل ابتدا باید به سرور SMTP دسترسی داشت؛ همچنین نیازمند سرور SMTP گفته شده به منظور ارسال ایمیل از درون خود است. برای کاربران رومینگ، اینکار نیازمند تغییر مداوم پیکربندی برنامه SMTP است چرا که سرور SMTP فالکوت پیام‌هایی را که نشانی‌های IP خارج از شرکت را داشته باشند رد می‌کند. دو راه حل موجود است: یا کاربر رومینگ سرور SMTP را روی رایانه خود نصب کند یا کماکان به شیوه‌ای از سرور شرکت استفاده کنند که به عنوان یک کارمند احرازهویت شوند. راه اول توصیه نمی‌شود چرا که رایانه ممکن است همیشه متصل نباشد و در صورت بروز مشکل امکان ارسال مجدد پیام وجود نخواهد داشت؛ ما روی راه حل دوم تمرکز می‌کنیم.
احرازهویت SMTP در Postfix نیازمند SASL یا Simple Authentication and Security Layer است. اینکار نیازمند نصب بسته‌های libsasl2-modules و sasl2-bin، سپس ثبت گذرواژه در پایگاه‌داده SASL برای هر کاربری که نیازمند احرازهویت با استفاده از سرور SMTP است. اینکار با استفاده از دستور saslpasswd2 انجام می‌شود که چندین پارامتر را دریافت می‌کند. گزینه -u دامنه احرازهویت را تعریف می‌کند که باید با پارامتر smtpd_sasl_local_domain در پیکربندی Postfix یکسان باشد. گزینه -c اجازه تعریف یک کاربر جدید و -f اجازه تعریف فایل پیکربندی آن را می‌دهد در صورتی که نیاز باشد پایگاه‌داده SASL در مسیر دیگری بجز /etc/sasldb2 قرار بگیرد.
# saslpasswd2 -u `postconf -h myhostname` -f /var/spool/postfix/etc/sasldb2 -c jean
[... type jean's password twice ...]
نکته اینکه پایگاه‌داده SASL در دایرکتوی Postfix ایجاده شده است. به منظور حصوص اطمینان از یکپارچگی، فایل /etc/sasldb2 را با دستور ln -sf /var/spool/postfix/etc/sasldb2 /etc/sasldb2 به یک پیوند نمادین پایگاه‌داده مورد استفاده Postfix تبدیل کرده‌ایم.
اکنون نیازمند پیکربندی Postfix برای استفاده از SALS هستیم. ابتدا کاربر postfix باید به گروه sasl اضافه گردد تا بتواند به پایگاه‌داده SASL دسترسی داشته باشد. برای فعال‌سازی SASL به چندین پارامتر دیگر نیاز است و پارامتر smtpd_recipient_restrictions نیز به منظور اجازه به برنامه‌های احرازهویت شده با SASL برای ارسال ایمیل، مورد نیاز است.

مثال 11.12. فعال‌سازی SASL در /etc/postfix/main.cf

# Enable SASL authentication
smtpd_sasl_auth_enable = yes
# Define the SASL authentication domain to use
smtpd_sasl_local_domain = $myhostname
[...]
# Adding permit_sasl_authenticated before reject_unauth_destination
# allows relaying mail sent by SASL-authenticated users
smtpd_recipient_restrictions =
    permit_sasl_authenticated,
    permit_mynetworks,
    reject_unauth_destination,
[...]
It is usually a good idea to not send passwords over an unencrypted connection. Postfix allows using different configurations for each port (service) it runs on. All these can be configured with different rules and directives in the /etc/postfix/master.cf file. To turn off authentication at all for port 25 (smtpd service) add the following directive:
smtp      inet  n       -       y       -       -       smtpd
    [..]
    -o smtpd_sasl_auth_enable=no
    [..]
If for some reason clients use an outdated AUTH command (some very old mail clients do), interoperability with them can be enabled using the broken_sasl_auth_clients directive.