认证流程【4】-http_callback_auth()函数

来源:互联网 发布:传承网络 编辑:程序博客网 时间:2024/06/04 19:14

void http_callback_auth(httpd *webserver, request *r)
{
    t_client    *client;
    httpVar * token;
    char    *mac;
    httpVar *logout = httpdGetVariableByName(r, "logout");

    if (wx_link_wifi == 1) {
        debug(LOG_DEBUG, "#####http_callback_auth#######: %s/%s\r\n", r->request.host, r->request.path);
        return;
    }
   
   debug(LOG_DEBUG, "!!!!http_callback_auth!!!\n");

     /* 检查http 报文里有没有token*/
    if ((token = httpdGetVariableByName(r, "token"))) {
        /* They supplied variable "token" */
          /* 然后通过arp_get 拿该报文发送者的ip 对应的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();
            /* 到之前提到的客户端链表里搜此ip 和mac 对应的客户端*/
            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) {
               /*客户端是logout请求(http报文请求里有logout)
                    网关会发一个关于此客户端下线的通知到认证服务器*/
                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);
                qos_del_user(client);
                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 {
                debug(LOG_DEBUG, "Client for %s is already in the client list", client->ip);
            }
            UNLOCK_CLIENT_LIST();
          /* 不是logout请求,那么不管是不是新的客户端,都需要再次认证一下*/
            if (!logout) {
                authenticate_client(r);
            }
            free(mac);
        }
    } else {
        /* They did not supply variable "token" */
        send_http_page(r, "WiFiDog error", "Invalid token");
    }
}
0 0
原创粉丝点击