wifidog 源码初分析(1)

来源:互联网 发布:hdfs如何查看数据字段 编辑:程序博客网 时间:2024/06/01 10:00

wifidog 的核心还是依赖于 iptables 防火墙过滤规则来实现的,所以建议对 iptables 有了了解后再去阅读 wifidog 的源码。

在路由器上启动 wifidog 之后,wifidog 在启动时会初始化一堆的防火墙规则,如下:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片

  1. /** Initialize the firewall rules
  2. */
  3. int iptables_fw_init(void)  
  4. {  
  5.     … …  
  6. /*
  7.      *
  8.      * Everything in the NAT table
  9.      *
  10.      */
  11. /* Create new chains */
  12.     iptables_do_command("-t nat -N " TABLE_WIFIDOG_OUTGOING);  
  13.     iptables_do_command("-t nat -N " TABLE_WIFIDOG_WIFI_TO_ROUTER);  
  14.     iptables_do_command("-t nat -N " TABLE_WIFIDOG_WIFI_TO_INTERNET);  
  15.     iptables_do_command("-t nat -N " TABLE_WIFIDOG_GLOBAL);  
  16.     iptables_do_command("-t nat -N " TABLE_WIFIDOG_UNKNOWN);  
  17.     iptables_do_command("-t nat -N " TABLE_WIFIDOG_AUTHSERVERS);  
  18. /* Assign links and rules to these new chains */
  19.     iptables_do_command("-t nat -A PREROUTING -i %s -j " TABLE_WIFIDOG_OUTGOING, config->gw_interface);  
  20.     iptables_do_command("-t nat -A " TABLE_WIFIDOG_OUTGOING " -d %s -j " TABLE_WIFIDOG_WIFI_TO_ROUTER, config->gw_address);  
  21.     iptables_do_command("-t nat -A " TABLE_WIFIDOG_WIFI_TO_ROUTER " -j ACCEPT");  
  22.     iptables_do_command("-t nat -A " TABLE_WIFIDOG_OUTGOING " -j " TABLE_WIFIDOG_WIFI_TO_INTERNET);  
  23.     iptables_do_command("-t nat -A " TABLE_WIFIDOG_WIFI_TO_INTERNET " -m mark --mark 0x%u -j ACCEPT", FW_MARK_KNOWN);  
  24.     iptables_do_command("-t nat -A " TABLE_WIFIDOG_WIFI_TO_INTERNET " -m mark --mark 0x%u -j ACCEPT", FW_MARK_PROBATION);  
  25.     iptables_do_command("-t nat -A " TABLE_WIFIDOG_WIFI_TO_INTERNET " -j " TABLE_WIFIDOG_UNKNOWN);  
  26.     iptables_do_command("-t nat -A " TABLE_WIFIDOG_UNKNOWN " -j " TABLE_WIFIDOG_AUTHSERVERS);  
  27.     iptables_do_command("-t nat -A " TABLE_WIFIDOG_UNKNOWN " -j " TABLE_WIFIDOG_GLOBAL);  
  28. // 将 80 端口的访问重定向(REDIRECT)到 (本路由)网关web服务器的监听端口
  29.     iptables_do_command("-t nat -A " TABLE_WIFIDOG_UNKNOWN " -p tcp --dport 80 -j REDIRECT --to-ports %d", gw_port);  
  30. /*
  31.      *
  32.      * Everything in the FILTER table
  33.      *
  34.      */
  35. /* Create new chains */
  36.     iptables_do_command("-t filter -N " TABLE_WIFIDOG_WIFI_TO_INTERNET);  
  37.     iptables_do_command("-t filter -N " TABLE_WIFIDOG_AUTHSERVERS);  
  38.     iptables_do_command("-t filter -N " TABLE_WIFIDOG_LOCKED);  
  39.     iptables_do_command("-t filter -N " TABLE_WIFIDOG_GLOBAL);  
  40.     iptables_do_command("-t filter -N " TABLE_WIFIDOG_VALIDATE);  
  41.     iptables_do_command("-t filter -N " TABLE_WIFIDOG_KNOWN);  
  42.     iptables_do_command("-t filter -N " TABLE_WIFIDOG_UNKNOWN);  
  43. /* Assign links and rules to these new chains */
  44. /* Insert at the beginning */
  45.     iptables_do_command("-t filter -I FORWARD -i %s -j " TABLE_WIFIDOG_WIFI_TO_INTERNET, config->gw_interface);  
  46.     iptables_do_command("-t filter -A " TABLE_WIFIDOG_WIFI_TO_INTERNET " -m state --state INVALID -j DROP");  
  47. /* TCPMSS rule for PPPoE */
  48.     iptables_do_command("-t filter -A " TABLE_WIFIDOG_WIFI_TO_INTERNET " -o %s -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu", ext_interface);  
  49.     iptables_do_command("-t filter -A " TABLE_WIFIDOG_WIFI_TO_INTERNET " -j " TABLE_WIFIDOG_AUTHSERVERS);  
  50.     iptables_fw_set_authservers();  
  51.     iptables_do_command("-t filter -A " TABLE_WIFIDOG_WIFI_TO_INTERNET " -m mark --mark 0x%u -j " TABLE_WIFIDOG_LOCKED, FW_MARK_LOCKED);  
  52.     iptables_load_ruleset("filter", "locked-users", TABLE_WIFIDOG_LOCKED);  
  53.     iptables_do_command("-t filter -A " TABLE_WIFIDOG_WIFI_TO_INTERNET " -j " TABLE_WIFIDOG_GLOBAL);  
  54.     iptables_load_ruleset("filter", "global", TABLE_WIFIDOG_GLOBAL);  
  55.     iptables_load_ruleset("nat", "global", TABLE_WIFIDOG_GLOBAL);  
  56.     iptables_do_command("-t filter -A " TABLE_WIFIDOG_WIFI_TO_INTERNET " -m mark --mark 0x%u -j " TABLE_WIFIDOG_VALIDATE, FW_MARK_PROBATION);  
  57.     iptables_load_ruleset("filter", "validating-users", TABLE_WIFIDOG_VALIDATE);  
  58.     iptables_do_command("-t filter -A " TABLE_WIFIDOG_WIFI_TO_INTERNET " -m mark --mark 0x%u -j " TABLE_WIFIDOG_KNOWN, FW_MARK_KNOWN);  
  59.     iptables_load_ruleset("filter", "known-users", TABLE_WIFIDOG_KNOWN);  
  60.     iptables_do_command("-t filter -A " TABLE_WIFIDOG_WIFI_TO_INTERNET " -j " TABLE_WIFIDOG_UNKNOWN);  
  61.     iptables_load_ruleset("filter", "unknown-users", TABLE_WIFIDOG_UNKNOWN);  
  62.     iptables_do_command("-t filter -A " TABLE_WIFIDOG_UNKNOWN " -j REJECT --reject-with icmp-port-unreachable");  
  63.     UNLOCK_CONFIG();  
  64. return 1;  

在该 防火墙规则的初始化过程中,会首先清除掉已有的防火墙规则,重新创建新的过滤链,另外,除了通过iptables_do_command("-t nat -A "TABLE_WIFIDOG_UNKNOWN " -p tcp --dport 80 -j REDIRECT --to-ports %d",gw_port); 这个命令将 接入设备的 80 端口(HTTP)的访问重定向至网关自身的 HTTP 的端口之外,还通过iptables_fw_set_authservers(); 函数设置了 鉴权服务器(auth-server) 的防火墙规则:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片

  1. void iptables_fw_set_authservers(void)  
  2. {  
  3. const s_config *config;  
  4.     t_auth_serv *auth_server;  
  5.     config = config_get_config();  
  6. for (auth_server = config->auth_servers; auth_server != NULL; auth_server = auth_server->next) {  
  7. if (auth_server->last_ip && strcmp(auth_server->last_ip, "0.0.0.0") != 0) {  
  8.             iptables_do_command("-t filter -A " TABLE_WIFIDOG_AUTHSERVERS " -d %s -j ACCEPT", auth_server->last_ip);  
  9.             iptables_do_command("-t nat -A " TABLE_WIFIDOG_AUTHSERVERS " -d %s -j ACCEPT", auth_server->last_ip);  
  10.         }  
  11.     }  

首先从上面的代码可以看出 wifidog 支持多个 鉴权服务器,并且针对每一个鉴权服务器 设置了如下两条规则:

1)在filter表中追加一条[任何访问鉴权服务器都被接受]的WiFiDog_$ID$_AuthServers过滤链:

iptables -t filter -A  WiFiDog_$ID$_AuthServers -d auth-server地址 -j ACCEPT

2)在nat表中追加一条[任何访问鉴权服务器都被接受]的WiFiDog_$ID$_AuthServers过滤链:

iptables -t nat -A WiFiDog_$ID$_AuthServers  -d auth-server地址 -j ACCEPT

这样确保可以访问鉴权服务器,而不是拒绝所有的出口访问。



wifidog 源码初分析(2)

上一篇分析了接入设备的首次浏览器访问请求如何通过 防火墙过滤规则 重定向到 wifidog 的 HTTP 服务中,本篇主要分析了 wifidog 在接收到 接入设备的 HTTP 访问请求后,如何将此 HTTP 请求重定向到 认证服务器(auth-server) 上。

通过上面的防火墙规则,会将通过上面的防火墙规则,会将HTTP请求的外部IP地址和端口通过NAT方式重定向至本地wifidog内嵌HTTP服务器的地址和端口上,并由内嵌HTTP服务器进行服务,而内嵌HTTP服务器的路径和回调处理如下:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片

  1. if ((webserver = httpdCreate(config->gw_address, config->gw_port)) == NULL) {  
  2.     debug(LOG_ERR, "Could not create web server: %s", strerror(errno));  
  3.     exit(1);  
  4. }  
  5. debug(LOG_DEBUG, "Assigning callbacks to web server");  
  6. httpdAddCContent(webserver, "/", "wifidog", 0, NULL, http_callback_wifidog);  
  7. httpdAddCContent(webserver, "/wifidog", "", 0, NULL, http_callback_wifidog);  
  8. httpdAddCContent(webserver, "/wifidog", "about", 0, NULL, http_callback_about);  
  9. httpdAddCContent(webserver, "/wifidog", "status", 0, NULL, http_callback_status);  
  10. httpdAddCContent(webserver, "/wifidog", "auth", 0, NULL, http_callback_auth);  
  11. httpdAddC404Content(webserver, http_callback_404); 

客户端首次访问时回调客户端首次访问时回调http_callback_404函数,在该函数中根据获取的客户端信息来配置重定向的URL fragment,如下:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片

  1. void
  2. http_callback_404(httpd *webserver, request *r)  
  3. {  
  4. char tmp_url[MAX_BUF],  
  5.             *url,  
  6.             *mac;  
  7.     s_config    *config = config_get_config();  
  8.     t_auth_serv *auth_server = get_auth_server();  
  9.     memset(tmp_url, 0, sizeof(tmp_url));  
  10. /*
  11.      * XXX Note the code below assumes that the client's request is a plain
  12.      * http request to a standard port. At any rate, this handler is called only
  13.      * if the internet/auth server is down so it's not a huge loss, but still.
  14.      */ /* 用户需要访问的URL */
  15.         snprintf(tmp_url, (sizeof(tmp_url) - 1), "http://%s%s%s%s",  
  16.                         r->request.host,  
  17.                         r->request.path,  
  18.                         r->request.query[0] ? "?" : "",  
  19.                         r->request.query);  
  20.     url = httpdUrlEncode(tmp_url);  
  21. if (!is_online()) {  
  22. /* 路由器都接入不到 internet */
  23. char * buf;  
  24.         send_http_page(r, "Uh oh! Internet access unavailable!", buf);  
  25.         free(buf);  
  26.     }  
  27. else if (!is_auth_online()) {  
  28. /* auth server 挂起 */
  29. char * buf;  
  30.         send_http_page(r, "Uh oh! Login screen unavailable!", buf);  
  31.         free(buf);  
  32.     }  
  33. else {  
  34. /* 配置重定向到 auth server 的 url 参数 */
  35. char *urlFragment;  
  36. if (!(mac = arp_get(r->clientAddr))) {  
  37. /* We could not get their MAC address */
  38.             debug(LOG_INFO, "Failed to retrieve MAC address for ip %s, so not putting in the login request", r->clientAddr);  
  39.             safe_asprintf(&urlFragment, "%sgw_address=%s&gw_port=%d&gw_id=%s&url=%s",  
  40.                 auth_server->authserv_login_script_path_fragment,  
  41.                 config->gw_address,  
  42.                 config->gw_port,  
  43.                 config->gw_id,  
  44.                 url);  
  45.         } else {            
  46.             debug(LOG_INFO, "Got client MAC address for ip %s: %s", r->clientAddr, mac);  
  47.             safe_asprintf(&urlFragment, "%sgw_address=%s&gw_port=%d&gw_id=%s&mac=%s&url=%s",  
  48.                 auth_server->authserv_login_script_path_fragment,  
  49.                 config->gw_address,  
  50.                 config->gw_port,  
  51.                 config->gw_id,  
  52.                 mac,  
  53.                 url);  
  54.         }  
  55. /* 调用该函数将用户请求重定向到 auth server 的登录页面 */
  56.         http_send_redirect_to_auth(r, urlFragment, "Redirect to login page");  
  57.         free(urlFragment);  
  58.     }  
  59.     free(url);  

上面代码基本不用解释,具体重定向至auth server的消息在下面的 http_send_redirect_to_auth 函数中实现:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片

  1. void http_send_redirect_to_auth(request *r, char *urlFragment, char *text)  
  2. {  
  3. char *protocol = NULL;  
  4. int port = 80;  
  5.     t_auth_serv *auth_server = get_auth_server();  
  6. if (auth_server->authserv_use_ssl) {  
  7.         protocol = "https";  
  8.         port = auth_server->authserv_ssl_port;  
  9.     } else {  
  10.         protocol = "http";  
  11.         port = auth_server->authserv_http_port;  
  12.     }  
  13. char *url = NULL;  
  14.     safe_asprintf(&url, "%s://%s:%d%s%s",  
  15.         protocol,  
  16.         auth_server->authserv_hostname,  
  17.         port,  
  18.         auth_server->authserv_path,  
  19.         urlFragment  
  20.     );  
  21.     http_send_redirect(r, url, text);  
  22.     free(url);  

具体的重定向URL给个实例:

POST /login/?gw_address=192.168.1.1&gw_port=2060&gw_id=default&mac=44:94:fc:ef:28:40&url=http%3A//www.baidu.com/ HTTP/1.1

可以看到这里有这几个参数信息:

2gw_address,路由器的LAN地址

2gw_port:为wifidog的监听端口

2gw_id:路由器的标识名

2mac:客户端设备的MAC地址

2url:为客户端访问的原URL(以便于重定向)



wifidog 源码初分析(3)


上一篇分析了 接入设备 在接入路由器,并发起首次 HTTP/80 请求到路由器上时,wifidog 是如何将此 HTTP 请求重定向至 auth-server 的流程。

之后 接入设备 的浏览器接收到 wifidog 返回的 302 重定向请求后,会将页面重定向至 auth-server 的 /login 页面,并且在此 URL 中会携带一些 路由器/网关 参数,以及 接入设备的 MAC 地址 和 客户端访问的源URL(如示例中的 baidu.com)。

下面几个步骤就是 接入设备 到 auth-server 上的认证过程,因本系列主要分析 wifidog 源码,这里只截取了 接入设备 与 auth-server 之间的通信报文:

http://s3.51cto.com/wyfs02/M02/23/30/wKiom1M0M_ahAWdiAAUXNsxbVZw932.jpg

本示例对应的 auth-server 是使用 authpuppy 搭建的认证服务器,且使用了 localUser 插件,该插件是需要用户输入用户名/密码的方式来认证的,下图即为输入正确的用户名/密码后,auth-server 返回重定向到 wifidog 的响应(注:同时携带了为此接入设备的用户分配了 token):

http://s3.51cto.com/wyfs02/M02/23/30/wKiom1M0NOKihesxAAWtknjdq9M275.jpg

同样的,接入设备的浏览器会继续重定向到 路由器的 wifidog 的 /wifidog/auth 服务上。

+

下一篇会继续分析 wifidog 如何进一步对 客户端 进行鉴权,并为此接入设备开启防火墙。


wifidog 源码初分析(4)


在上一篇《wifidog 源码处分析(3)》的流程结束后,接入设备的浏览器重定向至 路由器 上 wifidog 的 http 服务(端口 2060) /wifidog/auth 上(且携带了 认证服务器 为此接入设备分配的 token),本篇就是从 wifidog 接收到 /wifidog/auth 的访问后的 校验流程。

-

根据《wifidog 源码初分析(2)》中描述的,在 wifidog 启动 http 服务前,注册了一个针对访问路径 /wifidog/auth 的回调,如下:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片

  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在CODE上查看代码片派生到我的代码片

  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 信息的),类似如下:

[steven@sasd ~]$ 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

[steven@sasd ~]$

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在CODE上查看代码片派生到我的代码片

  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在CODE上查看代码片派生到我的代码片

  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 的)响应报文:

http://s3.51cto.com/wyfs02/M02/23/4B/wKioL1M1huOBxAg1AAQPxuinLeU316.jpg




0 0
原创粉丝点击