分享

国外个人作品介绍:构建个人私有云案例,保护数据隐私的控制权(4)

nettman 发表于 2015-4-3 22:00:45 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 0 12361
本帖最后由 nettman 于 2015-4-3 22:03 编辑

接上篇
国外个人作品介绍:构建个人私有云案例,保护数据隐私的控制权(3)


确保你发出的邮件能通过垃圾邮件过滤器
这个部分我们的目标是让我们的邮件服务器能尽量干净地出现在世界上,并让垃圾邮件发送者们更难以我们的名义发邮件。作为附加效果,这也有助于让我们的邮件能通过其他邮件服务器的垃圾邮件过滤器。

发送者策略框架(SPF)
发送者策略框架(SPF)是你添加到自己服务器区域里的一份记录,声明了整个因特网上哪些邮件服务器能以你的域名发邮件。设置非常简单,使用microsoft.com上的SPF向导来生成你的SPF记录,然后作为一个TXT记录添加到自己的服务器区域里。看上去像这样:

  1. jhausse.net.    300 IN  TXT v=spf1 mx mx:cloud.jhausse.net -all
复制代码


反向PTR
我们之前在本文里讨论过这个问题,建议你为自己的服务器正确地设置反向DNS,这样对服务器IP地址的反向查询能返回你服务器的实际名字。

OpenDKIM
当我们激活OpenDKIM后,postfix会用密钥为每封发出去的邮件签名。然后我们将把这个密钥存储在DNS域中。这样的话,世界上任意一个邮件服务器都能够检验邮件是否真的是我们发出的,或是由垃圾邮件发送者伪造的。让我们先安装opendkim:

  1. apt-get install opendkim opendkim-tools
复制代码


然后按如下方式编辑/etc/opendkim.conf文件的配置:

  1. ##
  2. ## opendkim.conf -- configuration file for OpenDKIM filter
  3. ##
  4. Canonicalization        relaxed/relaxed
  5. ExternalIgnoreList      refile:/etc/opendkim/TrustedHosts
  6. InternalHosts           refile:/etc/opendkim/TrustedHosts
  7. KeyTable                refile:/etc/opendkim/KeyTable
  8. LogWhy                  Yes
  9. MinimumKeyBits          1024
  10. Mode                    sv
  11. PidFile                 /var/run/opendkim/opendkim.pid
  12. SigningTable            refile:/etc/opendkim/SigningTable
  13. Socket                  inet:8891@localhost
  14. Syslog                  Yes
  15. SyslogSuccess           Yes
  16. TemporaryDirectory      /var/tmp
  17. UMask                   022
  18. UserID                  opendkim:opendkim
复制代码


我们还需要几个额外的文件,需保存在目录/etc/opendkim里:

  1. mkdir -pv /etc/opendkim/
  2. cd /etc/opendkim/
复制代码


让我们建立新文件/etc/opendkim/TrustedHosts并写入以下内容:

  1. 127.0.0.1
复制代码


建立新文件/etc/opendkim/KeyTable并写入以下内容:

  1. cloudkey jhausse.net:mail:/etc/opendkim/mail.private
复制代码


这会告诉OpenDKIM我们希望使用一个名叫'cloudkey'的加密密钥,它的内容在文件/etc/opendkim/mail.private里。我们建立另一个名叫/etc/opendkim/SigningTable的文件然后写入下面这一行:

  1. *@jhausse.net cloudkey
复制代码


这会告诉OpenDKIM每封从jhausse.net域发出的邮件都应该用'cloudkey'密钥签名。如果我们还有其他域希望也能签名,我们也可以在这里添加。

下一步是生成密钥并修改OpenDKIM配置文件的权限。

  1. opendkim-genkey -r -s mail [-t]
  2. chown -Rv opendkim:opendkim /etc/opendkim
  3. chmod 0600 /etc/opendkim/*
  4. chmod 0700 /etc/opendkim
复制代码


一开始,最好使用-t开关,这样会通知其他邮件服务器你只是在测试模式下,这样他们就不会丢弃基于你的OpenDKIM签名的邮件(目前来说)。你可以从mail.txt文件里看到OpenDKIM密钥:

  1. cat mail.txt
复制代码


然后把它作为一个TXT记录添加到区域文件里,应该是类似这样的:

  1. mail._domainkey.cloud1984.net.  300 IN TXT  v=DKIM1; k=rsa; p=MIGfMA0GCSqG...
复制代码


最后,我们需要告诉postfix来为发出的邮件签名。在文件/etc/postfix/main.cf末尾,添加:

  1. # Now for OpenDKIM: we'll sign all outgoing emails
  2. smtpd_milters = inet:127.0.0.1:8891
  3. non_smtpd_milters = $smtpd_milters
  4. milter_default_action = accept
复制代码


然后重启相关服务:

  1. service postfix reload
  2. service opendkim restart
复制代码


测试
现在让我们测试一下是否能找到我们的OpenDKIM公钥并和私钥匹配:

  1. opendkim-testkey -d jhausse.net -s mail -k mail.private -vvv
复制代码


这个应该返回:

  1. opendkim-testkey: key OK
复制代码


这个你可能需要等一会直到域名服务器重新加载该区域(对于Linode,每15分钟会更新一次)。你可以用dig来检查区域是否已经重新加载。

如果这个没问题,让我们测试一下其他服务器能验证我们的OpenDKIM签名和SPF记录。要做这个,我们可以用Brandon Checkett的邮件测试系统。发送一封邮件到Brandon的网页上提供的测试地址,我们可以在服务器上运行下面的命令
  1. mail -s CloudCheck ihAdmTBmUH@www.brandonchecketts.com
复制代码



在Brandon的网页上,我们应该可以在'DKIM Signature'部分里看到result = pass的文字,以及在'SPF Information'部分看到Result: pass的文字。如果我们的邮件通过这个测试,只要不加-t开关重新生成OpenDKIM密钥,上传新的密钥到区域文件里,然后重新测试检查是否仍然可以通过这些测试。如果可以的话,恭喜!你已经在你的服务器上成功配置好OpenDKIM和SPF了!



使用Owncloud提供日历,联系人,文件服务并通过Roundcube配置网页邮件
既然我们已经拥有了一流的邮件服务器,让我们再为它增加在云上保存通讯录,日程表和文件的能力。这些是Owncloud所提供的非常赞的服务。在这个弄好后,我们还会设置一个网页邮件,这样就算你没带任何电子设备出去旅行时,或者说在你的手机或笔记本没电的情况下,也可以通过网吧来检查邮件。

安装Owncloud非常直观,而且在这里有非常好的介绍。在Debian系统里,归根结底就是把owncloud的仓库添加到apt源里,下载Owncloud的发行密钥并安装到apt钥匙链中,然后通过apt-get安装Owncloud:

  1. echo 'deb http://download.opensuse.org/repositories/isv:/ownCloud:/community/Debian_7.0/ /' >> /etc/apt/sources.list.d/owncloud.list
  2. wget http://download.opensuse.org/repositories/isv:ownCloud:community/Debian_6.0/Release.key
  3. apt-key add - < Release.key
  4. apt-get update
  5. apt-get install apache2 owncloud roundcube
复制代码


在有提示的时候,选择dbconfig并设置roundcube使用mysql。然后,提供一下mysql的root密码并为roundcube的mysql用户设置一个漂亮的密码。然后,按如下方式编辑roundcube的配置文件/etc/roundcube/main.inc.php,这样登录roundcube默认会使用你的IMAP服务器:

  1. $rcmail_config['default_host'] = 'ssl://localhost';
  2. $rcmail_config['default_port'] = 993;
复制代码


现在我们来配置一下apache2网页服务器增加SSL支持,这样我们可以和Owncloud和Roundcube对话时使用加密的方式传输我们的密码和数据。让我们打开Apache的SSL模块:

  1. a2enmod ssl
复制代码


然后编辑文件/etc/apache2/ports.conf并设定以下参数:

  1. NameVirtualHost *:80
  2. Listen 80
  3. ServerName www.jhausse.net
  4. <IfModule mod_ssl.c>
  5.     # If you add NameVirtualHost *:443 here, you will also have to change
  6.     # the VirtualHost statement in /etc/apache2/sites-available/default-ssl
  7.     # to <VirtualHost *:443>
  8.     # Server Name Indication for SSL named virtual hosts is currently not
  9.     # supported by MSIE on Windows XP.
  10.     NameVirtualHost *:443
  11.     Listen 443
  12. </IfModule>
  13. <IfModule mod_gnutls.c>
  14.     Listen 443
  15. </IfModule>
复制代码


我们将在目录/var/www下为服务器加密连接https://www.jhausse.net设定一个默认网站。编辑文件/etc/apache2/sites-available/default-ssl:

  1. <VirtualHost _default_:443>
  2.         ServerAdmin webmaster@localhost
  3.         DocumentRoot /var/www
  4.         ServerName www.jhausse.net
  5.         [...]
  6.         <Directory /var/www/owncloud>
  7.           Deny from all
  8.         </Directory>
  9.         [...]
  10.         SSLCertificateFile    /etc/ssl/certs/cloud.crt
  11.         SSLCertificateKeyFile /etc/ssl/private/cloud.key
  12.         [...]
  13. </VirtualHost>
  14. 然后让我们同时也在目录/var/www下设定一个非加密连接http://www.jhausse.net的默认网站。编辑文件/etc/apache2/sites-available/default:
  15. <VirtualHost _default_:443>
  16.         DocumentRoot /var/www
  17.         ServerName www.jhausse.net
  18.         [...]
  19.         <Directory /var/www/owncloud>
  20.           Deny from all
  21.         </Directory>
  22. </VirtualHost>
复制代码


这样的话,我们通过把文件放到/var/www目录下让www.jhausse.net使用它们提供网站服务。名叫'Deny from all'的指令可以阻止通过www.jhausse.net访问Owncloud:我们将设定通过https://cloud.jhausse.net来正常访问。

现在我们将设定网页邮件(roundcube),让它可以通过网址https://webmail.jhausse.net来访问。编辑文件/etc/apache2/sites-available/roundcube并写入以下内容:

  1. <IfModule mod_ssl.c>
  2. <VirtualHost *:443>
  3.         ServerAdmin webmaster@localhost
  4.         DocumentRoot /var/lib/roundcube
  5.     # The host name under which you'd like to access the webmail
  6.         ServerName webmail.jhausse.net
  7.         <Directory />
  8.                 Options FollowSymLinks
  9.                 AllowOverride None
  10.         </Directory>
  11.         ErrorLog ${APACHE_LOG_DIR}/error.log
  12.         # Possible values include: debug, info, notice, warn, error, crit,
  13.         # alert, emerg.
  14.         LogLevel warn
  15.         CustomLog ${APACHE_LOG_DIR}/ssl_access.log combined
  16.         #   SSL Engine Switch:
  17.         #   Enable/Disable SSL for this virtual host.
  18.         SSLEngine on
  19.         # do not allow unsecured connections
  20.         # SSLRequireSSL
  21.         SSLCipherSuite HIGH:MEDIUM
  22.         #   A self-signed (snakeoil) certificate can be created by installing
  23.         #   the ssl-cert package. See
  24.         #   /usr/share/doc/apache2.2-common/README.Debian.gz for more info.
  25.         #   If both key and certificate are stored in the same file, only the
  26.         #   SSLCertificateFile directive is needed.
  27.         SSLCertificateFile    /etc/ssl/certs/cloud.crt
  28.         SSLCertificateKeyFile /etc/ssl/private/cloud.key
  29.         # Those aliases do not work properly with several hosts on your apache server
  30.         # Uncomment them to use it or adapt them to your configuration
  31.         Alias /program/js/tiny_mce/ /usr/share/tinymce/www/
  32.         # Access to tinymce files
  33.         <Directory "/usr/share/tinymce/www/">
  34.                 Options Indexes MultiViews FollowSymLinks
  35.                 AllowOverride None
  36.                 Order allow,deny
  37.                 allow from all
  38.         </Directory>
  39.         <Directory /var/lib/roundcube/>
  40.                 Options +FollowSymLinks
  41.                 # This is needed to parse /var/lib/roundcube/.htaccess. See its
  42.                 # content before setting AllowOverride to None.
  43.                 AllowOverride All
  44.                 order allow,deny
  45.                 allow from all
  46.         </Directory>
  47.         # Protecting basic directories:
  48.         <Directory /var/lib/roundcube/config>
  49.                 Options -FollowSymLinks
  50.                 AllowOverride None
  51.         </Directory>
  52.         <Directory /var/lib/roundcube/temp>
  53.                 Options -FollowSymLinks
  54.                 AllowOverride None
  55.                 Order allow,deny
  56.                 Deny from all
  57.         </Directory>
  58.         <Directory /var/lib/roundcube/logs>
  59.                 Options -FollowSymLinks
  60.                 AllowOverride None
  61.                 Order allow,deny
  62.                 Deny from all
  63.         </Directory>
  64.         <FilesMatch "\.(cgi|shtml|phtml|php)[        DISCUZ_CODE_20        ]quot;>
  65.                 SSLOptions +StdEnvVars
  66.         </FilesMatch>
  67.         <Directory /usr/lib/cgi-bin>
  68.                 SSLOptions +StdEnvVars
  69.         </Directory>
  70.         #   SSL Protocol Adjustments:
  71.         #   The safe and default but still SSL/TLS standard compliant shutdown
  72.         #   approach is that mod_ssl sends the close notify alert but doesn't wait for
  73.         #   the close notify alert from client. When you need a different shutdown
  74.         #   approach you can use one of the following variables:
  75.         #   o ssl-unclean-shutdown:
  76.         #     This forces an unclean shutdown when the connection is closed, i.e. no
  77.         #     SSL close notify alert is send or allowed to received.  This violates
  78.         #     the SSL/TLS standard but is needed for some brain-dead browsers. Use
  79.         #     this when you receive I/O errors because of the standard approach where
  80.         #     mod_ssl sends the close notify alert.
  81.         #   o ssl-accurate-shutdown:
  82.         #     This forces an accurate shutdown when the connection is closed, i.e. a
  83.         #     SSL close notify alert is send and mod_ssl waits for the close notify
  84.         #     alert of the client. This is 100% SSL/TLS standard compliant, but in
  85.         #     practice often causes hanging connections with brain-dead browsers. Use
  86.         #     this only for browsers where you know that their SSL implementation
  87.         #     works correctly.
  88.         #   Notice: Most problems of broken clients are also related to the HTTP
  89.         #   keep-alive facility, so you usually additionally want to disable
  90.         #   keep-alive for those clients, too. Use variable "nokeepalive" for this.
  91.         #   Similarly, one has to force some clients to use HTTP/1.0 to workaround
  92.         #   their broken HTTP/1.1 implementation. Use variables "downgrade-1.0" and
  93.         #   "force-response-1.0" for this.
  94.         BrowserMatch "MSIE [2-6]" \
  95.                 nokeepalive ssl-unclean-shutdown \
  96.                 downgrade-1.0 force-response-1.0
  97.         # MSIE 7 and newer should be able to use keepalive
  98.         BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown
  99. </VirtualHost>
  100. </IfModule>
复制代码


然后在你的DNS服务商那里声明一下服务器,例如:

webmail.jhausse.net.    300 IN  CNAME   cloud.jhausse.net.
现在让我激活这三个网站:

a2ensite default default-ssl roundcube
service apache2 restart
关于网页邮件,可以通过网址https://webmail.jhausse.net来访问,基本上能工作。之后使用邮箱全名(例如roudy@jhausse.net)和在本文一开始在邮件服务器数据库里设定的密码登录。第一次连接成功,浏览器会警告说证书没有可靠机构的签名。这个没什么关系,只要添加一个例外即可。

最后但很重要的是,我们将通过把以下内容写入到/etc/apache2/sites-available/owncloud来为Owncloud创建一个虚拟主机。

  1. <IfModule mod_ssl.c>
  2. <VirtualHost *:443>
  3.         ServerAdmin webmaster@localhost
  4.         DocumentRoot /var/www/owncloud
  5.         ServerName cloud.jhausse.net
  6.         <Directory />
  7.                 Options FollowSymLinks
  8.                 AllowOverride None
  9.         </Directory>
  10.         <Directory /var/www/owncloud>
  11.                 Options Indexes FollowSymLinks MultiViews
  12.                 AllowOverride All
  13.                 Order allow,deny
  14.                 allow from all
  15.         </Directory>
  16.         ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
  17.         <Directory "/usr/lib/cgi-bin">
  18.                 AllowOverride None
  19.                 Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
  20.                 Order allow,deny
  21.                 Allow from all
  22.         </Directory>
  23.         ErrorLog ${APACHE_LOG_DIR}/error.log
  24.         # Possible values include: debug, info, notice, warn, error, crit,
  25.         # alert, emerg.
  26.         LogLevel warn
  27.         CustomLog ${APACHE_LOG_DIR}/ssl_access.log combined
  28.         #   SSL Engine Switch:
  29.         #   Enable/Disable SSL for this virtual host.
  30.         SSLEngine on
  31.         # do not allow unsecured connections
  32.         # SSLRequireSSL
  33.         SSLCipherSuite HIGH:MEDIUM
  34.         SSLCertificateFile    /etc/ssl/certs/cloud.crt
  35.         SSLCertificateKeyFile /etc/ssl/private/cloud.key
  36.         <FilesMatch "\.(cgi|shtml|phtml|php)[        DISCUZ_CODE_21        ]quot;>
  37.                 SSLOptions +StdEnvVars
  38.         </FilesMatch>
  39.         <Directory /usr/lib/cgi-bin>
  40.                 SSLOptions +StdEnvVars
  41.         </Directory>
  42.         BrowserMatch "MSIE [2-6]" \
  43.                 nokeepalive ssl-unclean-shutdown \
  44.                 downgrade-1.0 force-response-1.0
  45.         # MSIE 7 and newer should be able to use keepalive
  46.         BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown
  47. </VirtualHost>
  48. </IfModule>
复制代码


然后通过执行以下命令激活Owncloud:
  1. a2ensite owncloud
  2. service apache2 reload
复制代码


之后通过在浏览器里打开链接https://cloud.jhausse.net/配置一下Owncloud。

就这些了!现在你已经拥有自己的Google Drive,日程表,联系人,Dropbox,以及Gmail!好好享受下新鲜恢复保护的隐私吧!:-)




在云上同步你的设备
要同步你的邮件,你只需用你喜欢的邮件客户端即可:Android或iOS自带的默认邮件应用,k9mail,或者电脑上的Thunderbird。或者你也可以使用我们设置好的网页邮件。

在Owncloud的文档里描述了如何与云端同步你的日程表和联系人。在Android系统中,我用的是CalDAV-Sync,CardDAV-Sync应用桥接了手机上Android自带日历以及联系人应用和Owncloud服务器。

对于文件,有一个叫Owncloud的Android应用可以访问你手机上的文件,然后自动把你拍的图片和视频上传到云中。在你的Mac/PC上访问云端文件也很容易,在Owncloud文档里有很好的描述。

最后一点提示
在上线后的前几个星期里,最好每天检查一下日志/var/log/syslog和/var/log/mail.log以保证一切都在顺利运行。在你邀请其他人(朋友,家人,等等)加入你的服务器之前这很重要。他们信任你能很好地架设个人服务器维护他们的数据,但是如果服务器突然崩溃会让他们很失望。

要添加另一个邮件用户,只要在数据库mailserver的virtual_users表中增加一行。

要添加一个域名,只要在virtual_domains表中增加一行。然后更新/etc/opendkim/SigningTable为发出的邮件签名,上传OpenDKIM密钥到服务器区域,然后重启OpenDKIM服务。

Owncloud有自己的用户数据库,在用管理员帐号登录后可以修改。

最后,万一在服务器临时崩溃的时候想办法找解决方案很重要。比如说,在服务器恢复之前你的邮件应该送往哪儿?一种方式是找个能帮你做备份MX的朋友,同时你也可以当他的备份MX(看下postfix的配置文件main.cf里relay_domains和relay_recipient_maps里的设定)。与此类似,如果你的服务器被破解然后一个坏蛋把你所有文件删了怎么办?对于这个,考虑增加一个常规备份系统就很重要了。Linode提供了备份选项。在1984.is里,我用crontabs和scp做了一个基本但管用的自动备份系统。

相关内容:

国外个人作品介绍:构建个人私有云案例,保护数据隐私的控制权(1)
国外个人作品介绍:构建个人私有云案例,保护数据隐私的控制权(2)
国外个人作品介绍:构建个人私有云案例,保护数据隐私的控制权(3)
国外个人作品介绍:构建个人私有云案例,保护数据隐私的控制权(4)





via: https://www.howtoforge.com/tutor ... d-on-debian-wheezy/

作者:Roudy Jhausse 译者:zpl1025 校对:wxy

本文由 LCTT 原创翻译,Linux中国 荣誉推出




加微信w3aboutyun,可拉入技术爱好者群

没找到任何评论,期待你打破沉寂

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条