使用 PAM 作為 Apache Basic 驗證

Apache內建是沒有系統帳戶驗證的,要透過third-party模組和pam模組來進行驗證。
這篇文章我會使用到mod_authnz_external和pwauth這兩款程式/模組。

首先要安裝pwauth,紅帽系列可以用epel直接安裝,其他發行版就...不要問我XD

好了以後,下載合適的mod_authnz_external。繼續往下之前要先提醒,記得安裝apache開發套件(httpd-devel),和各種開發相關軟體(gcc, libtool之類的),不然沒辦法進行編譯。

解開封裝後,資料夾內會有mod_authnz_external.c,透過下面這個指令來做編譯

apxs -c mod_authnz_external.c

如果在apache 2.4環境編譯時遇到這種錯誤,不用擔心,只是API有更動,將c檔案內的「remote_ip」改成「client_ip」或「useragent_ip」,詳細的部分參閱官方API

mod_authnz_external.c: In function 'exec_external':
mod_authnz_external.c:446: error: 'conn_rec' has no member named 'remote_ip'
mod_authnz_external.c:447: error: 'conn_rec' has no member named 'remote_ip'
apxs:Error: Command failed with rc=65536

編譯好之後,使用這段指令安裝模組

apxs -i -a mod_authnz_external.la

安裝完可以在apache module資料夾看到mod_authnz_external.so這個檔案。
如果是在開發環境編譯完手動拿到營運環境使用,搬移完記得修改擁有者root、權限755和selinux內文(system_u:object_r:httpd_modules_t:s0),如此才能正確載入。

載入只需要在httpd.conf使用LoadModule。

設定則是分為兩部分,一部分是定義外部驗證者,另一部分是定義驗證條件。

定義外部驗證只需一行,通常放在<VirtualHost>就好,這邊用pwauth示範,完整用法見官方wiki

DefineExternalAuth sysauth pipe /usr/bin/pwauth

最後剩下使用<directory>來設定要驗證的資料夾就完成了

AuthType Basic
AuthName "Enter your username and password."
AuthBasicProvider external #指定使用mod_authnz_external
AuthExternal sysauth #指定使用上面定義的sysauth
Require valid-user

資料來源:
https://code.google.com/p/mod-auth-external/issues/detail?id=8
http://kishantha.blogspot.tw/2009/03/apache-library-permission-denied-in.html
http://icephoenix.us/linuxunix/apache-and-http-authentication-with-pam/

留言

粗體斜體刪除線連結引用圖片程式碼

注意:您的電子信箱將不會被公開,且網站連結不會被搜尋引擎採計

{124} {123} {122} {121} {120} {119} {118} {117} {116} {115} {114} {113} {112} {111} {100} {025} {024} {023} {022} {021} {020} {019} {018} {017} {016} {015} {014} {013} {012} {011} {010} {009} {008} {007} {006} {005} {004} {003} {002} {001}