2014年9月3日 星期三

AWS 筆記 - 使用 Amazon Route53 設定 DNS SPF Record

如果說 MX 是用來提供寄件者得知 mail server 在哪邊,那 SPF 則是提供 mail receiver 驗證寄件者使用的 mail server 是不是接近已知、合法的 server。因此,設定 DNS SPF Record 是會降低信件誤判成垃圾郵件的機率。

採用 Amazon Route53 時,設定 TXT Record (有 SPF Record,但這次沒試)

"v=spf1 a mx a:YourServerHostname  ~all"

最後面的 ~all 代表上述未定義的,可能是錯的。若要強制歸類在錯誤,就用 -all 即可。

驗證方式(此例以 cs.nctu.edu.tw 為例):

$ nslookup -q=txt cs.nctu.edu.tw
...
cs.nctu.edu.tw  text = "v=spf1 a mx a:farewell.cs.nctu.edu.tw a:csmailer.cs.nctu.edu.tw a:tcsmailer.cs.nctu.edu.tw a:csmailgate.cs.nctu.edu.tw a:csmail1.cs.nctu.edu.tw a:csmail2.cs.nctu.edu.tw a:csws1.cs.nctu.edu.tw a:csws2.cs.nctu.edu.tw ~all"
...


可以看到 "v=spf1 ... " 資訊,代表 DNS TXT Record 設定無誤,記得這是有 dns cache 機制,不見得馬上更改或是馬上可以查詢到。

接著,可以用 Gmail 來測試,從自家 mail server 寄信到 Gmail,若沒有設定 DNS SPF Record ,會顯示:

Received-SPF: none (google.com: YourSender@YourMailServer does not designate permitted sender hosts) client-ip=YourMailServerIP;

若有設定 DNS SPF Record 但不在名單內,此例為 ~all 用法:

Received-SPF: softfail (google.com: domain of transitioning YourSender@YourMailServer does not designate YourMailServerIP as permitted sender) client-ip=YourMailServerIP;

若設定正確即在 DNS SPF Record 定義內:

Received-SPF: pass (google.com: domain of YourSender@YourMailServer designates YourMailServerIP as permitted sender) client-ip=YourMailServerIP;

要留意,如果租的機器是在一些常見的 VPS 時,對外走的可能是 IPv6 ,此時記得 DNS Reocrd 中,也要用 AAAA Record (IPv6) 定義。

2014年9月2日 星期二

[Linux] 透過 alias 與 virtual 機制建立 no-reply @ hostname 帳號 @ Postfix 2.9.6, Ubuntu 12.04

新增一筆 alias 用來導向 /dev/null

$ grep alias_maps /etc/postfix/main.cf
alias_maps = hash:/etc/aliase
$ sudo vim /etc/aliases
...
devnull: /dev/null
...


採用 virtual account 新增 no-reply 帳號:

$ grep virtual_alias_maps /etc/postfix/main.cf
$ sudo vim /etc/postfix/main.cf
virtual_alias_maps = hash:/etc/postfix/virtual

$ sudo vim /etc/postfix/virtual
no-reply@hostname devnull

$ sudo newaliases
$ sudo postmap /etc/postfix/virtual
$ sudo postfix reload
postfix/postfix-script: refreshing the Postfix mail system


至於測試方式,就直接連他寄信看看:

$ telnet localhost 25
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 hostname ESMTP Postfix (Ubuntu)
HELO localhost
250 hostname
mail from:<root@localhost>
250 2.1.0 Ok
rcpt to:<no-reply@hostname>
250 2.1.0 Ok
quit
221 2.0.0 Bye
Connection closed by foreign host.


如果出現 451 4.3.0 <no-reply@hostname>: Temporary lookup failure ,請記得翻一下 /var/log/mail.err ,若是單純 error: open database /etc/postfix/virtual.db: No such file or directory 問題,記得跑一下 sudo postmap /etc/postfix/virtual 即可。

2014年8月28日 星期四

[Python] Google App Engines 發送 POST 與 JSON-RPC

初始資料:

url = 'http://server/cgi'
data = {
"arg1" : "val1",
"arg2" : "val2"
}


發送 POST:

import urllib
from google.appengine.api import urlfetch
post_data = urllib.urlencode(data)
response = urlfetch.fetch(url=url,payload=post_data,method=urlfetch.POST,headers={'Content-Type': 'application/x-www-form-urlencoded'})


發送 JSONRPC:

import urllib2
headers = {
"Content-Type": "application/json"
}
post_data = json.dumps(data)
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req).read()

2014年8月27日 星期三

[PHP] Mantis Plugin 開發筆記 @ Mantis 1.2.17

由於公司管理者熟悉 Mantis 系統,最近挑了 Mantis 來做類似 Customer Relationship Management (CRM) 服務,接著很容易碰到 User Group 的需求,這是在系統本身不存在的功能。

在網路上找了許久,有發現 User Group 相關的 Plugin,但是拿出來搭配最新版 Mantis 1.2.17 時,出現很多 Link 失效、或是某些表單送出時,導向回來失敗等。由於 trace/fix 完就等於把整個 plugin 做完了,就順便把 Mantis Plugin 開發筆記起來 Orz

首先,在 Mantis 架構下,有提供 Plugin 開發機制,提供在系統任何一個端點加入 Event Notification 架構,透過這個 notification 導入 Plugin 程式碼,以 User Group 來說,就是管理者在編輯使用者帳號時,希望也有張表可以查看目前使用者歸屬的 User Group Management,以及簡易的編輯、刪除等。

其中 Mantis Event 架構可以查詢:mantis/core/event_api.php,而 Mantis Plugin 架構可參考:mantis/core/plugin_api.php。開發 Mantis Plugin 文件,請參考這份:Building a Plugin,看完大概也了解了。

整體流程:
  1. 寫個 class ExamplePlugin extends MantisPlugin 
  2. 定義 plugin 需要的 db schema
  3. 綁定 event 串接自己定義的 function
其他心得:
  • 若 plugin db schema 想要變更時,除了刪除既定存在的外,還需要去刪除 mantis_config_table 裡頭的記錄,例如刪掉 config_id 等價於自己 plugin 名稱才行。如此一來,plugin 重新安裝時,才會重新建立對應的 table

2014年8月25日 星期一

[PHP] Mantis Email Notification Settings @ Ubuntu 12.04

在 Ubuntu 12.04 透過 apt-get 安裝後,配置好系統管理後,又手動把它生成 Mantis 1.2.17 了。預設 Mantis 是有啟動 Email Notification,包含新增帳號後,還要由對方 email 去設定密碼等。對於 Email Notification 的設定,如事件通知的寄信者資訊等,都是直接更改 /path/mantis/config_inc.php。

可以參考 http://www.mantisbt.org/manual/admin.config.email.htmlhttp://www.mantisbt.org/manual/admin.customize.email.html,簡易筆記:

//$g_enable_email_notification = OFF;
$g_mail_receive_own = OFF;
$g_default_notify_flags = array(
'reporter' => ON,
'handler' => ON,
'monitor' => ON,
'bugnotes' => ON,
'threshold_min' => NOBODY,
'threshold_max' => NOBODY
);

//
// Select the method to mail by: PHPMAILER_METHOD_MAIL for use of mail() function, PHPMAILER_METHOD_SENDMAIL for sendmail (or postfix), PHPMAILER_METHOD_SMTP for SMTP. Default is PHPMAILER_METHOD_MAIL.
//
$g_phpMailer_method = PHPMAILER_METHOD_MAIL;
$g_smtp_host = 'localhost';
$g_smtp_username = '';
$g_smtp_password = '';
$g_administrator_email = 'admin@example.com';
$g_from_email = 'noreply@example.com';
$g_from_name = 'Mantis Bug Tracker';