Monthly Archive一月 2008
網路科技 dada on 一月 27, 2008
Using Flickr API with phpFlickr
本來我的私人照片都是放到自己架的網站上面的,一直在考慮要不要改放到 Flickr 上面...
最重要的一個考量因素就是與我自己的網站程式容不容易整合
雖然利用 Yahoo 送的免費三個月 Flickr Pro 帳號已經把幾年來、數十本相簿統統丟上去備份了,但一直沒有時間去實驗一下 Flickr API 到底合不合我用
拖了一陣子,結果免費三個月的 Flickr Pro 帳號在今天終於到期了,而且 Flickr Gift 買一年送三個月(備註)的活動也即將在一月底結束了,因此今天還是花了點時間評估了一下 Flickr API,結果還蠻滿意的,這幾天應該就會刷卡買了吧
我用的是 phpFlickr 這套程式庫,使用起來還蠻容易的,所有 Flickr 提供的 API 都有支援,而我要的其實只是列出所有相簿(sets)列表而已。唯一有點可惜的是,Flickr API 還沒有支援珍藏集 (collections),目前僅能到相簿(sets)這個層次而已,但這不是 phpFlickr 的問題
使用 phpFlickr 前,要先去生一個 Flickr API Key 出來,然後就可以用這個 Key 來連接 Flickr API 了
要用 phpFlickr 程式去使用 Flickr API 需要認證,有兩種模式:
1. 每次都去認證使用者的帳號,phpFlickr 會把你導到 Flickr 的認證頁面再導回來
先把 API Key 的 callback 指到 phpFlickr/auth.php,然後程式呼叫 auth() 後再作想作的事
1 2 3 4 5 6 7 8 9 10 11 12 | <? include('phpFlickr/phpFlickr.php'); $f = new phpFlickr("[API Key]","[Auth Secret]"); // 填入 API Key 及 Secret $f->auth(); // 作自己的事 print_r( $f->photosets_getList() ); ?> |
呼叫 auth() 會把使用者瀏覽器導到 Flickr 的認證網頁,而程式本身會結束執行:
認證網頁可以讓使用者授權讓你的服務去存取使用者的 Flickr 帳號,不一定是上面產生 API key 的那個帳號。當認證完畢後,Flickr 會再把使用者瀏覽器導回之前設定的 callback (phpFlickr/auth.php),然後再導回你原本呼叫 auth() 的那隻程式
再導回這個程式時,會有相關認證資訊 (authentication token) 放在 session data 中,這時呼叫 auth() 就不再會把使用者導到其他網頁了,而是會回傳認證的權限狀況,接下來程式就可以作自己的事了
使用者隨時可以經由 這個網頁 來取消對你的程式的授權:
2. 每次都是用固定的帳號去認證,不會導到 Flickr 認證頁面
要使用這種作法,請參考 http://www.phpflickr.com/tools/auth/ 去產生一個永久的 token,接下來程式就可以直接使用了:
1 2 3 4 5 6 7 8 9 10 11 12 | <? include('phpFlickr/phpFlickr.php'); $f = new phpFlickr("[API Key]","[Auth Secret]"); // 填入 API Key 及 Secret $f->setToken("[Token]"); // 填入上面產生的 token // 作自己的事 print_r( $f->photosets_getList() ); ?> |
-----
複雜的認證過後,你就可以開始玩 Flickr API 了... phpFlickr 支援所有的 Flickr API,詳細列表請直接參照 API 說明文件
例如要得到所有相簿列表必須要呼叫 flickr.photosets.getList 這個 API,在 phpFlickr 裡面就直接呼叫 $f->photosets_getList() 這個函數即可。也就是把 API 名字中的 "flickr." 拿掉,並把 . 換成 _ 就是 phpFlickr 支援的函數名稱了!
更詳細的說明請直接參閱 phpFlickr 的網站: http://phpflickr.com/
--
[備註]
Flickr 實際上並沒有買一年送三個月的活動,但是現在可以買一年 Flickr Pro 帳號送別人,自己也會得到免費三個月的 Flickr Pro 帳號,而這個活動並沒有限制你不能送給自己,所以等於是買一年送三個月。
想參加這個活動的人千萬不要直接去買一年喔,一定要參加 Flickr Gift 活動 "送禮物給自己" 才有多送三個月。
這個活動於太平洋時間 2008 年 1 月 31 日晚上 11時59分 (GMT-8) 結束,也就是台北時間 2008 年 2 月 1 日下午 3時59分 (GMT+8),想買 Flickr Pro 的人請把握時間囉...
硬體 dada on 一月 19, 2008
SDHC support on Thinkpad X60
Thinkpad X60 原本並沒有 支援 SDHC 卡,把 SDHC 卡插入 X60 左方的 SD 插槽會讀不到
本來以為 X60 對於 SDHC 的支援是無望了,沒想到現在竟然可以了,而且還只要更新作業系統即可... 詳情請參考微軟發佈的 Hotfix 923293
(標題: 在 Windows XP Hotfix 923293 為高容量的 SD 記憶卡加入支援)
不過這個 Hotfix 微軟還沒有釋放出來讓大家下載安裝,未來新的 Service Pack 3 才會放進去,有需要的人必須自己去線上申請,選擇好你的作業系統版本及語言即可
申請之後,微軟就會把 Hotfix 的下載連結寄給你(還有個密碼來解壓縮),正體中文 WinXP 的 Hotfix 檔名為: WindowsXP-KB923293-v4-x86-CHT.exe,怕麻煩的話網路上搜尋一下應該找得到.. 或者寄信給我吧...
安裝此 Hotfix、重新開機後,果然可以讀到 SDHC 卡了! 同事的華碩 ASUS W5 (WinXP) 原本也是不能讀 SDHC 卡,服用這帖 Hotfix 後也 OK 了
Yeah! 這樣方便多了
2008/04/30 update: Windows XP SP3 已經包含這個修正了,不需要再特別安裝 hotfix 了...
資訊安全 dada on 一月 07, 2008
SSH Keyboard-Interactive Authentication
有些 SSH clients (例如: SecureCRT)允許使用者「儲存」密碼,這對系統安全是個大忌,一旦 client 遭受入侵,server 也會暴露在危險之中。
建議直接在伺服器端取消 SSH 的 password authentication 功能,改用 keyboard interactive 的方式認證,通常這樣的話,client 就無法儲存密碼了:
1. OpenSSH - /etc/ssh/sshd_config
PasswordAuthentication no ChallengeResponseAuthentication yes
2. SSH2 (Tectia) - /etc/ssh2/sshd2_config
AllowedAuthentications publickey,keyboard-interactive AuthKbdInt.Optional pam,password AuthKbdInt.Required password
修改好上述 sshd 的設定檔後,送個 HUP 給 sshd 即可(注意不要不小心把目前用的 session 也砍了,不然改錯的話只好到 console 前面去救了)
# ps ax | grep sbin/sshd | grep Ss 9767 ? Ss 0:00 /usr/sbin/sshd # kill -HUP 9767
然後就可以請大家改用 keyboard-interactive authentication 了:
資訊安全 dada on 一月 03, 2008
SSH2 vs OpenSSH
常見的 SSH Implementation 有兩種,ssh.com 的 SSH 以及 OpenSSH,詳細發展原由可參考 OpenSSH History
由於歷史因素,所以提到 SSH 相關用語時,常因不明確而混淆。建議可以參考 O'Reilly book 出版的 SSH, The Secure Shell: The Definitive Guide 一書中對於 SSH 各種詞彙的精確定義:
Terminology: SSH Protocols and Products
SSH
A generic term referring to SSH protocols or software products.
SSH-1
The SSH protocol, Version 1. This protocol went through several
revisions, of which 1.3 and 1.5 are the best known, and we will
write SSH-1.3 and SSH-1.5 should the distinction be necessary.
SSH-2
The SSH protocol, Version 2, as defined by several draft standards
documents of the IETF SECSH working group.
SSH1
Tatu Ylönen's software implementing the SSH-1 protocol; the original
SSH. Now distributed and maintained (minimally) by SSH
Communications Security, Inc.
SSH2
The "SSH Secure Shell" product from SSH Communications Security, Inc.
This is a commercial SSH-2 protocol implementation, though it is
licensed free of charge in some circumstances.
ssh (all lowercase letters)
A client program included in SSH1, SSH2, OpenSSH, F-Secure SSH, and
other products, for running secure terminal sessions and remote
commands. In SSH1 and SSH2, it is also named ssh1/ssh2, respectively.
OpenSSH
The product OpenSSH from the OpenBSD project,
which implements both the SSH-1 and SSH-2 protocols.
OpenSSH/1
OpenSSH, referring specifically to its behavior
when using the SSH-1 protocol.
OpenSSH/2
OpenSSH, referring specifically to its behavior
when using the SSH-2 protocol.
OpenSSH 基於相容 BSD license 的 ssh 1.2.12 來開發,後續又加上了對 SSH-2 的支援
但 OpenSSH 對 SSH-2 的實作與 ssh.com 後來對 SSH-2 的實作已經有了一些差異。
現今大部分的 open source 作業系統都已經改用 OpenSSH 了,不過有些地方還是可以看到 SSH2 的蹤跡,可能是商業版本或是非商業版本,例如:
# ssh -V ssh: SSH Secure Shell 3.2.9.1 (non-commercial version) on i686-pc-linux-gnu
SSH Communications Security, Inc. (www.ssh.com) 的商業版本 SSH (SSH2) 正式名稱叫做 Tectia SSH client/server
熟悉 OpenSSH 的人初次接觸 SSH2 最常遇到的問題就是 Public-Key Authentication 的作法以及 Key format 都不太一樣。
OpenSSH/1 與 SSH1 的 authorization file 都是放在 ~/.ssh/authorized_keys 這個檔案之內,identity 則是放在 ~/.ssh/identity 這個檔案內
OpenSSH/2 的 authorization file 一樣是 ~/.ssh/authorized_keys,identity 則是在 ~/.ssh/id_dsa 或 ~/.ssh/id_rsa 內,依據 key type 而異。但其實因為 OpenSSH/2 有向下相容 OpenSSH/1 的功能,所以事實上也會參考 ~/.ssh/identity 來當作 SSH-1 的 identity
SSH2 就很不一樣了。SSH2 允許有多個 identity,因此會有一個檔案 ~/.ssh/identification 可以設定有哪些 identity 可用,例如:
IdKey identity-1 IdKey identity-2 IdKey identity-3
真正的 private key 放在 identity-1, identity-2, identity-3 這三個檔案內,而使用 ssh -i 指定 identity 時,不同於 OpenSSH 是直接指到 key 本身,SSH2 必須指定這個間接的 identification file
SSH2 的 public key format 也與 OpenSSH 不同,不再是一行而已,也因此 authorization file 沒辦法跟 ~/.ssh/authorized_keys 一樣一行放一個 key 值。
SSH2 也是一樣使用一個檔案來設定允許的 public key 有哪些,檔案放在 ~/.ssh/authorization
Key identity-1.pub Key identity-2.pub Key identity-3.pub
這個檔案表示可接受三個 public key 分別為 identity-1.pub, identity-2.pub, identity-3.pub
而如同 OpenSSH 一樣,SSH2 的 authorization 檔案內也可以指定各種 options,例如:
Key identity-1.pub Options no-port-forwarding,no-pty Key identity-2.pub Options command="rsync -az --server . /home/backup-x",no-pty
詳細資訊可參考 SSH2 的 manual
SSH2 的 key format 與 OpenSSH 不同,這在兩者之間要作 public key authentication 時造成了一些麻煩。OpenSSH 提供的 ssh-keygen 可以用 -e 和 -i 兩個選項來轉換兩者的 key format:
# ssh-keygen -e -f openssh-key.pub > ssh2-key.pub # ssh-keygen -i -f ssh2-key.pub > openssh-key.pub
OpenSSH 與 SSH2 之間的問題還不只這樣,SCP 的實作方式也不同
OpenSSH 與 SSH1 的 SCP 是 RCP over SSH,而 SSH2 附的 SCP2 背地裡則是用 SFTP 實作的,因此兩者互相 copy 東西時有時會 copy 不過去,建議還是一律改用有經過 IETF 標準認證的 SFTP 就好了
如果可以的話,全部用 OpenSSH 是最單純的,不過有時不是自己能控制的,碰到使用 SSH2 的系統,其實只要稍微了解一下差異也很容易適應就是了
參考資料:
SSH Escape Character
SSH client 有一個 Escape Character (跳脫字元),事實上 rsh/rlogin 也有支援 Escape Character (~),就如同 Ctrl-] 之於 telnet 一樣...
SSH client 的 Escape Character 一般跟 rsh/rlogin 一樣設定成 ~,詳細用法可於命令列按 ~? 來取得說明,要注意的是,為了避免影響一般正常的輸入,Escape Character 必須是命令列換行後的第一個按鍵,如果你已經輸入別的按鍵,即使按 backspace 把游標移回開頭處再輸入 Escape Character 也是無效的!請先按 Enter 換行後再輸入吧!
myhost# ~? Supported escape sequences: ~. - terminate connection ~B - send a BREAK to the remote system ~C - open a command line ~R - Request rekey (SSH protocol 2 only) ~^Z - suspend ssh ~# - list forwarded connections ~& - background ssh (when waiting for connections to terminate) ~? - this message ~~ - send the escape character by typing it twice (Note that escapes are only recognized immediately after newline.)
最常用的 Escape Character 就是 ~. 以及 ~^Z 這兩個:
~. 是直接切斷目前的 SSH 連線,如果你遇到遠端機器沒有反應時,可用此方式來切斷連線
~^Z (~ 及 Ctrl-Z) 則是把目前的 SSH 連線先 suspend,一般可用來回到原來機器上處理一些事情,若要再回去,輸入 fg 即可
問題來了,假設你從 HOST1 先 SSH 到 HOST2,然後再從 HOST2 SSH 到 HOST3
HOST1 ===1===> HOST2 ===2===> HOST3
如果你在 HOST3 的 terminal 下輸入 ~. 的話,你會直接切斷上面的第一段連線回到 HOST1 的 terminal,當然這樣的話第二段連線也一併 bye-bye 了!如果只是想切斷第二段連線回到 HOST2 該怎麼作呢?
這時候 ~~ 就發揮作用了,你只要輸入 ~~. 就可以切斷第二段連線了
以此累推,輸入 N 個 Escape Character 代表對第 N 段連線送出 Escape Character
最後,若不想用 ~ 當作跳脫字元,可於命令列用 -e 修改,例如把 SSH 跳脫字元改成 #:
ssh -e "#" myhost.mydomain.com


