wifidog 源码初分析(三)

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

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

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

auth-server 收到请求后处理,并返回重定向到 wifidog 的响应(注:同时携带了为此接入设备的用户分配了 token),接入设备的浏览器重定向至路由器上 wifidog 的 http 服务(端口 2060) /wifidog/auth 上(且携带了认证服务器为此接入设备分配的 token),下面介绍下 wifidog 接收到 /wifidog/auth 的访问后的校验流程。

在 wifidog 启动 http 服务前,注册了一个针对访问路径 /wifidog/auth 的回调,如下:

httpdAddCContent(webserver, "/wifidog", "about", 0, NULL, http_callback_about);

httpdAddCContent(webserver, "/wifidog", "status", 0, NULL, http_callback_status);

// 注册了针对 /wifidog/auth 的访问回调 http_callback_auth

httpdAddCContent(webserver, "/wifidog", "auth", 0, NULL, http_callback_auth);

这样对于 接入设备(or 客户端) 重定向过来的 /wifidog/auth 就进入了 http_callback_auth 函数中,如下:

http_callback_auth(httpd *webserver, request *r)

{

t_client    *client;

httpVar * token;

char    *mac;

// 1, 获取条件参数中的 logout 值

httpVar *logout = httpdGetVariableByName(r, "logout");

// 2, 获取条件参数中的 token 值

if ((token = httpdGetVariableByName(r, "token"))) {

/* They supplied variable "token" */

// 3, 可以看到, 这里要求必须能够通过 ARP 协议获取到 接入设备 的 MAC 地址

if (!(mac = arp_get(r->clientAddr))) {

/* We could not get their MAC address */

debug(LOG_ERR, "Failed to retrieve MAC address for ip %s", r->clientAddr);

send_http_page(r, "WiFiDog Error", "Failed to retrieve your MAC address");

} else {

/* We have their MAC address */

LOCK_CLIENT_LIST();

// 4, 检查该客户端(接入设备)是否已经在 wifidog 维护的接入客户端列表中

if ((client = client_list_find(r->clientAddr, mac)) == NULL) {

debug(LOG_DEBUG, "New client for %s", r->clientAddr);

client_list_append(r->clientAddr, mac, token->value);

} else if (logout) {

// 5, 退出处理

t_authresponse  authresponse;

s_config *config = config_get_config();

unsigned long long incoming = client->counters.incoming;

unsigned long long outgoing = client->counters.outgoing;

char *ip = safe_strdup(client->ip);

char *urlFragment = NULL;

t_auth_serv *auth_server = get_auth_server();

fw_deny(client->ip, client->mac, client->fw_connection_state);

client_list_delete(client);

debug(LOG_DEBUG, "Got logout from %s", client->ip);

/* Advertise the logout if we have an auth server */

if (config->auth_servers != NULL) {

UNLOCK_CLIENT_LIST();

auth_server_request(&authresponse, REQUEST_TYPE_LOGOUT, ip, mac, token->value,

incoming, outgoing);

LOCK_CLIENT_LIST();

/* Re-direct them to auth server */

debug(LOG_INFO, "Got manual logout from client ip %s, mac %s, token %s"

"- redirecting them to logout message", client->ip, client->mac, client->token);

safe_asprintf(&urlFragment, "%smessage=%s",

auth_server->authserv_msg_script_path_fragment,

GATEWAY_MESSAGE_ACCOUNT_LOGGED_OUT

);

http_send_redirect_to_auth(r, urlFragment, "Redirect to logout message");

free(urlFragment);

}

free(ip);

}

else {

// 6, 已经登录校验通过

debug(LOG_DEBUG, "Client for %s is already in the client list", client->ip);

}

UNLOCK_CLIENT_LIST();

if (!logout) {

// 7, 到 auth server 上进一步校验 token

authenticate_client(r);

}

free(mac);

}

} else {

/* They did not supply variable "token" */

// 8, 未携带 token, 直接拒绝

send_http_page(r, "WiFiDog error", "Invalid token");

}

}

在该函数中主要处理了 客户端退出,非法校验,以及 客户端校验等流程,下面分别描述注释中的各个步骤:

1,对于客户端退出,则会携带 logout 参数信息,并走到第 5 步(当然,如果连 token 参数都没有的话,会直接走到第 8 步,也就是拒绝);

2,按照正常的认证流程,会携带由认证服务器分配的 token 参数;

3,正如注释说明的,这里要求必须能够通过 ARP 协议获取到 接入设备 的 MAC 地址;(其实通过查看 arg_get 的实现,可以看到是直接解析 /proc/net/arp 文件 -- ARP cache -- 来获取对应客户端 IP 地址的 MAC 信息的),类似如下:

[asd@ubuntu ~]#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

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 函数中:

/** Authenticates a single client against the central server and returns when done

* Alters the firewall rules depending on what the auth server says

@param r httpd request struct

*/

void

authenticate_client(request *r)

{

t_client *client;

t_authresponse auth_response;

char *mac,

*token;

char *urlFragment = NULL;

s_config *config = NULL;

t_auth_serv *auth_server = NULL;

LOCK_CLIENT_LIST();

client = client_list_find_by_ip(r->clientAddr);

if (client == NULL) {

debug(LOG_ERR, "Could not find client for %s", r->clientAddr);

UNLOCK_CLIENT_LIST();

return;

}

mac = safe_strdup(client->mac);

token = safe_strdup(client->token);

UNLOCK_CLIENT_LIST();

/*

* At this point we've released the lock while we do an HTTP request since it could

* take multiple seconds to do and the gateway would effectively be frozen if we

* kept the lock.

*/

auth_server_request(&auth_response, REQUEST_TYPE_LOGIN, r->clientAddr, mac, token, 0, 0);

LOCK_CLIENT_LIST();

/* can't trust the client to still exist after n seconds have passed */

client = client_list_find(r->clientAddr, mac);

if (client == NULL) {

debug(LOG_ERR, "Could not find client node for %s (%s)", r->clientAddr, mac);

UNLOCK_CLIENT_LIST();

free(token);

free(mac);

return;

}

free(token);

free(mac);

/* Prepare some variables we'll need below */

config = config_get_config();

auth_server = get_auth_server();

switch(auth_response.authcode) {

case AUTH_ERROR:

/* Error talking to central server */

debug(LOG_ERR, "Got %d from central server authenticating token %s from %s at %s", auth_response, client->token, client->ip, client->mac);

send_http_page(r, "Error!", "Error: We did not get a valid answer from the central server");

break;

case AUTH_DENIED:

/* Central server said invalid token */

debug(LOG_INFO, "Got DENIED from central server authenticating token %s from %s at %s - redirecting them to denied message", client->token, client->ip, client->mac);

safe_asprintf(&urlFragment, "%smessage=%s",

auth_server->authserv_msg_script_path_fragment,

GATEWAY_MESSAGE_DENIED

);

http_send_redirect_to_auth(r, urlFragment, "Redirect to denied message");

free(urlFragment);

break;

case AUTH_VALIDATION:

/* They just got validated for X minutes to check their email */

debug(LOG_INFO, "Got VALIDATION from central server authenticating token %s from %s at %s"

"- adding to firewall and redirecting them to activate message", client->token,

client->ip, client->mac);

client->fw_connection_state = FW_MARK_PROBATION;

fw_allow(client->ip, client->mac, FW_MARK_PROBATION);

safe_asprintf(&urlFragment, "%smessage=%s",

auth_server->authserv_msg_script_path_fragment,

GATEWAY_MESSAGE_ACTIVATE_ACCOUNT

);

http_send_redirect_to_auth(r, urlFragment, "Redirect to activate message");

free(urlFragment);

break;

case AUTH_ALLOWED:

/* Logged in successfully as a regular account */

debug(LOG_INFO, "Got ALLOWED from central server authenticating token %s from %s at %s - "

"adding to firewall and redirecting them to portal", client->token, client->ip, client->mac);

client->fw_connection_state = FW_MARK_KNOWN;

fw_allow(client->ip, client->mac, FW_MARK_KNOWN);

served_this_session++;

safe_asprintf(&urlFragment, "%sgw_id=%s",

auth_server->authserv_portal_script_path_fragment,

config->gw_id

);

http_send_redirect_to_auth(r, urlFragment, "Redirect to portal");

free(urlFragment);

break;

case AUTH_VALIDATION_FAILED:

/* Client had X minutes to validate account by email and didn't = too late */

debug(LOG_INFO, "Got VALIDATION_FAILED from central server authenticating token %s from %s at %s "

"- redirecting them to failed_validation message", client->token, client->ip, client->mac);

safe_asprintf(&urlFragment, "%smessage=%s",

auth_server->authserv_msg_script_path_fragment,

GATEWAY_MESSAGE_ACCOUNT_VALIDATION_FAILED

);

http_send_redirect_to_auth(r, urlFragment, "Redirect to failed validation message");

free(urlFragment);

break;

default:

debug(LOG_WARNING, "I don't know what the validation code %d means for token %s from %s at %s - sending error message", auth_response.authcode, client->token, client->ip, client->mac);

send_http_page(r, "Internal Error", "We can not validate your request at this time");

break;

}

UNLOCK_CLIENT_LIST();

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中实现的,如下:

/* Set if a specific client has access through the firewall */

int iptables_fw_access(fw_access_t type, const char *ip, const char *mac, int tag)

{

int rc;

fw_quiet = 0;

switch(type) {

case FW_ACCESS_ALLOW:

iptables_do_command("-t mangle -A " TABLE_WIFIDOG_OUTGOING " -s %s -m mac --mac-source %s -j MARK --set-mark %d", ip, mac, tag);

rc = iptables_do_command("-t mangle -A " TABLE_WIFIDOG_INCOMING " -d %s -j ACCEPT", ip);

break;

case FW_ACCESS_DENY:

iptables_do_command("-t mangle -D " TABLE_WIFIDOG_OUTGOING " -s %s -m mac --mac-source %s -j MARK --set-mark %d", ip, mac, tag);

rc = iptables_do_command("-t mangle -D " TABLE_WIFIDOG_INCOMING " -d %s -j ACCEPT", ip);

break;

default:

rc = -1;

break;

}

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

最后,Auth server重定向客户端浏览器到www.baidu.com

本文由http://www.wifidog.pro/2014/12/08/wifidog-%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90.html整理编辑,转载请注明出处

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,012评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,589评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,819评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,652评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,954评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,381评论 1 210
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,687评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,404评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,082评论 1 238
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,355评论 2 241
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,880评论 1 255
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,249评论 2 250
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,864评论 3 232
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,007评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,760评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,394评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,281评论 2 259

推荐阅读更多精彩内容