wifidog 源碼初分析(4)-轉

來源:互聯網
上載者:User

標籤:blog   code   http   com   get   width   

在上一篇《wifidog 源碼處分析(3)》的流程結束後,接入裝置的瀏覽器重新導向至 路由器 上 wifidog 的 http 服務(連接埠 2060) /wifidog/auth 上(且攜帶了 證明伺服器 為此接入裝置分配的 token),本篇就是從 wifidog 接收到 /wifidog/auth 的訪問後的 校正流程。

-

根據《wifidog 源碼初分析(2)》中描述的,在 wifidog 啟動 http 服務前,註冊了一個針對訪問路徑 /wifidog/auth 的回調,如下:

[cpp] view plaincopy

  1. httpdAddCContent(webserver, "/wifidog", "about", 0, NULL, http_callback_about);  
  2. httpdAddCContent(webserver, "/wifidog", "status", 0, NULL, http_callback_status);  
  3. // 註冊了針對 /wifidog/auth 的訪問回調 http_callback_auth
  4. httpdAddCContent(webserver, "/wifidog", "auth", 0, NULL, http_callback_auth); 

這樣對於 接入裝置(or 用戶端) 重新導向過來的 /wifidog/auth 就進入了 http_callback_auth 函數中,如下:

[cpp] view plaincopy

  1. http_callback_auth(httpd *webserver, request *r)  
  2. {  
  3.     t_client    *client;  
  4.     httpVar * token;  
  5. char    *mac;  
  6. // 1, 擷取條件參數中的 logout 值
  7.     httpVar *logout = httpdGetVariableByName(r, "logout");  
  8. // 2, 擷取條件參數中的 token 值
  9. if ((token = httpdGetVariableByName(r, "token"))) {  
  10. /* They supplied variable "token" */
  11. // 3, 可以看到, 這裡要求必須能夠通過 ARP 協議擷取到 接入裝置 的 MAC 位址
  12. if (!(mac = arp_get(r->clientAddr))) {  
  13. /* We could not get their MAC address */
  14.             debug(LOG_ERR, "Failed to retrieve MAC address for ip %s", r->clientAddr);  
  15.             send_http_page(r, "WiFiDog Error", "Failed to retrieve your MAC address");  
  16.         } else {  
  17. /* We have their MAC address */
  18.             LOCK_CLIENT_LIST();  
  19. // 4, 檢查該用戶端(接入裝置)是否已經在 wifidog 維護的接入用戶端列表中
  20. if ((client = client_list_find(r->clientAddr, mac)) == NULL) {  
  21.                 debug(LOG_DEBUG, "New client for %s", r->clientAddr);  
  22.                 client_list_append(r->clientAddr, mac, token->value);  
  23.             } else if (logout) {  
  24. // 5, 退出處理
  25.                 t_authresponse  authresponse;  
  26.                 s_config *config = config_get_config();  
  27.                 unsigned long long incoming = client->counters.incoming;  
  28.                 unsigned long long outgoing = client->counters.outgoing;  
  29. char *ip = safe_strdup(client->ip);  
  30. char *urlFragment = NULL;  
  31.                 t_auth_serv *auth_server = get_auth_server();  
  32.                 fw_deny(client->ip, client->mac, client->fw_connection_state);  
  33.                 client_list_delete(client);  
  34.                 debug(LOG_DEBUG, "Got logout from %s", client->ip);  
  35. /* Advertise the logout if we have an auth server */
  36. if (config->auth_servers != NULL) {  
  37.                     UNLOCK_CLIENT_LIST();  
  38.                     auth_server_request(&authresponse, REQUEST_TYPE_LOGOUT, ip, mac, token->value,  
  39.                                         incoming, outgoing);  
  40.                     LOCK_CLIENT_LIST();  
  41. /* Re-direct them to auth server */
  42.                     debug(LOG_INFO, "Got manual logout from client ip %s, mac %s, token %s"
  43. "- redirecting them to logout message", client->ip, client->mac, client->token);  
  44.                     safe_asprintf(&urlFragment, "%smessage=%s",  
  45.                         auth_server->authserv_msg_script_path_fragment,  
  46.                         GATEWAY_MESSAGE_ACCOUNT_LOGGED_OUT  
  47.                     );  
  48.                     http_send_redirect_to_auth(r, urlFragment, "Redirect to logout message");  
  49.                     free(urlFragment);  
  50.                 }  
  51.                 free(ip);  
  52.             }  
  53. else {  
  54. // 6, 已經登入校正通過
  55.                 debug(LOG_DEBUG, "Client for %s is already in the client list", client->ip);  
  56.             }  
  57.             UNLOCK_CLIENT_LIST();  
  58. if (!logout) {  
  59. // 7, 到 auth server 上進一步校正 token
  60.                 authenticate_client(r);  
  61.             }  
  62.             free(mac);  
  63.         }  
  64.     } else {  
  65. /* They did not supply variable "token" */
  66. // 8, 未攜帶 token, 直接拒絕
  67.         send_http_page(r, "WiFiDog error", "Invalid token");  
  68.     }  

在該函數中主要處理了 用戶端退出,非法校正,以及 用戶端校正等流程,下面分別描述注釋中的各個步驟:

-

1,對於用戶端退出,則會攜帶 logout 參數資訊,並走到第 5 步(當然,如果連 token 參數都沒有的話,會直接走到第 8 步,也就是拒絕);

2,按照正常的認證流程,會攜帶由證明伺服器分配的 token 參數;

3,正如注釋說明的,這裡要求必須能夠通過 ARP 協議擷取到 接入裝置 的 MAC 位址;(其實通過查看 arg_get 的實現,可以看到是直接解析 /proc/net/arp 檔案 -- ARP cache -- 來擷取對應用戶端 IP 位址的 MAC 資訊的),類似如下:

[[email protected] ~]$ more /proc/net/arp

IP address       HW type     Flags       HW address            Mask     Device

192.168.1.203    0x1         0x2         18:03:73:d5:1b:a2     *        eth0

192.168.1.1      0x1         0x2         00:21:27:63:c0:ce     *        eth0

[[email protected] ~]$

4,在能夠擷取到該用戶端的 MAC 位址後,根據用戶端的 IP 和 MAC 位址檢查該用戶端是否已經在 wifidog 維護的接入裝置(or用戶端)列表中,如果不在,則追加到此列表中(關於此列表的資料結構在後面再詳細描述);

5,如果該用戶端已經存在,且本次訪問是要求 logout 退出的,則進入此退出處理的流程,該流程主要包括幾個步驟:關閉該用戶端 ip/mac 的出口(outgoing)規則 --> 從用戶端列表中刪除該用戶端記錄 --> 通知 證明伺服器 該用戶端退出(且攜帶該用戶端的token, 上下行流量等資訊) --> 返回重新導向至 證明伺服器 的 #define DEFAULT_AUTHSERVMSGPATHFRAGMENT "gw_message.php?" 訪問路徑(攜帶一個已退出的 message);

6,如果該用戶端已經登入校正過,且本次訪問非 logout 退出,則直接跳轉到第 7 步;

7,這一步就是 token 校正的過程,具體實現在 authenticate_client 函數中:

[cpp] view plaincopy

  1. authenticate_client(request *r)  
  2. {  
  3.     t_client    *client;  
  4.     t_authresponse  auth_response;  
  5. char    *mac,  
  6.         *token;  
  7. char *urlFragment = NULL;  
  8.     s_config    *config = NULL;  
  9.     t_auth_serv *auth_server = NULL;  
  10.     LOCK_CLIENT_LIST();  
  11. // 根據 IP 位址擷取 用戶端的 MAC 位址以及本次會話分配的 token
  12. // 主要用於 token 校正過程
  13.     client = client_list_find_by_ip(r->clientAddr);  
  14. if (client == NULL) {  
  15.         debug(LOG_ERR, "authenticate_client(): Could not find client for %s", r->clientAddr);  
  16.         UNLOCK_CLIENT_LIST();  
  17. return;  
  18.     }  
  19.     mac = safe_strdup(client->mac);  
  20.     token = safe_strdup(client->token);  
  21.     UNLOCK_CLIENT_LIST();  
  22. /*
  23.      * At this point we‘ve released the lock while we do an HTTP request since it could
  24.      * take multiple seconds to do and the gateway would effectively be frozen if we
  25.      * kept the lock.
  26.      */
  27. // 通過 "login" 到 證明伺服器 上進行用戶端的 token 校正
  28.     auth_server_request(&auth_response, REQUEST_TYPE_LOGIN, r->clientAddr, mac, token, 0, 0);  
  29.     LOCK_CLIENT_LIST();  
  30. /* can‘t trust the client to still exist after n seconds have passed */
  31. // 這裡主要防止在到 證明伺服器 上進行 token 校正的過程中
  32. // 該用戶端已經退出的情形, 此時就不需要再進行處理
  33.     client = client_list_find(r->clientAddr, mac);  
  34. if (client == NULL) {  
  35.         debug(LOG_ERR, "authenticate_client(): Could not find client node for %s (%s)", r->clientAddr, mac);  
  36.         UNLOCK_CLIENT_LIST();  
  37.         free(token);  
  38.         free(mac);  
  39. return;  
  40.     }  
  41.     free(token);  
  42.     free(mac);  
  43. /* Prepare some variables we‘ll need below */
  44.     config = config_get_config();  
  45.     auth_server = get_auth_server();  
  46. // 根據返回的校正結果做不同的處理
  47. switch(auth_response.authcode) {  
  48. case AUTH_ERROR:  
  49. case AUTH_DENIED:  
  50. case AUTH_VALIDATION:  
  51. case AUTH_VALIDATION_FAILED:  
  52.         ... ...  
  53. break;  
  54. case AUTH_ALLOWED:  
  55. /* Logged in successfully as a regular account */
  56.         debug(LOG_INFO, "Got ALLOWED from central server authenticating token %s from %s at %s - "
  57. "adding to firewall and redirecting them to portal", client->token, client->ip, client->mac);  
  58.         client->fw_connection_state = FW_MARK_KNOWN;  
  59.         fw_allow(client->ip, client->mac, FW_MARK_KNOWN);  
  60.         served_this_session++;  
  61.         safe_asprintf(&urlFragment, "%sgw_id=%s",  
  62.             auth_server->authserv_portal_script_path_fragment,  
  63.             config->gw_id  
  64.         );  
  65.         http_send_redirect_to_auth(r, urlFragment, "Redirect to portal");  
  66.         free(urlFragment);  
  67. break;  
  68.     }  
  69.     UNLOCK_CLIENT_LIST();  
  70. return;  

這裡主要是兩大步驟:

-

1,通過調用 auth_server_request(&auth_response, REQUEST_TYPE_LOGIN, r->clientAddr, mac, token, 0, 0); 讓 證明伺服器 對該用戶端的 token 進行校正;

2,根據 證明伺服器 返回的 token 校正結果進行不同的處理(主要是對該用戶端的防火牆過濾規則進行不同的設定),這裡主要以 AUTH_ALLOWED 校正結果進行分析,這裡主要是兩個動作:

2.1,通過 fw_allow 函數調用對此用戶端"允許存取";

2.2,返回重新導向至 證明伺服器的 portal 路徑訪問的響應;

-

這裡就簡要分析一下 fw_allow 函數的實現,查看fw_allow的實現可以看到真正設定allow用戶端通過防火牆的動作是在iptables_fw_access中實現的,如下:

[cpp] view plaincopy

  1. /** Set if a specific client has access through the firewall */
  2. // 針對上面的流程,這裡的輸入參數
  3. // type 為 FW_ACCESS_ALLOW,tag 為 FW_MARK_KNOWN
  4. int iptables_fw_access(fw_access_t type, const char *ip, const char *mac, int tag)  
  5. {  
  6. int rc;  
  7.     fw_quiet = 0;  
  8. switch(type) {  
  9. case FW_ACCESS_ALLOW:  
  10.             iptables_do_command("-t mangle -A " TABLE_WIFIDOG_OUTGOING " -s %s -m mac --mac-source %s -j MARK --set-mark %d", ip, mac, tag);  
  11.             rc = iptables_do_command("-t mangle -A " TABLE_WIFIDOG_INCOMING " -d %s -j ACCEPT", ip);  
  12. break;  
  13. case FW_ACCESS_DENY:  
  14.             iptables_do_command("-t mangle -D " TABLE_WIFIDOG_OUTGOING " -s %s -m mac --mac-source %s -j MARK --set-mark %d", ip, mac, tag);  
  15.             rc = iptables_do_command("-t mangle -D " TABLE_WIFIDOG_INCOMING " -d %s -j ACCEPT", ip);  
  16. break;  
  17. default:  
  18.             rc = -1;  
  19. break;  
  20.     }  
  21. return rc;  

同樣的,我們這裡主要分析一下ALLOW時的iptables的防火牆設定規則,對執行的兩個iptables命令展開來就是下面兩個步驟:

-

1) 在mangle表中追加WiFiDog_$ID$_Outgoing外出過濾鏈,該鏈的規則如下幾條:

   a) IP 位址為該用戶端的IP地址;

   b) MAC地址為該用戶端的MAC地址;

   c) 設定MARK為FW_MARK_KNOWN;

-

iptables –t mangle –AWiFiDog_$ID$_Outgoing  -s 用戶端IP地址 -m mac --mac-source 用戶端MAC地址 -j MARK --set-markFW_MARK_KNOWN

-

2)在mangle表中追加一條[接受所有目的地址為此用戶端IP地址的] WifiDog_$ID$_Incoming輸入過濾鏈;

-

iptables -t mangle -AWiFiDog_$ID$_Incoming -d 用戶端IP地址 -j ACCEPT

-

最後,看一下 wifidog 返回的重新導向請求到 證明伺服器 的請求報文 以及 證明伺服器 返回給 用戶端的(重新導向到原始訪問 baidu.com 的)響應報文:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.