From 99b4c9c6786a545711f93a60505785c592303bc2 Mon Sep 17 00:00:00 2001
From: Wichert Akkerman <wichert@wiggy.net>
Date: Mon, 21 Apr 2008 17:24:44 +0200
Subject: [PATCH] Add a basic disconnect command.

---
 src/gateway.c |    1 +
 src/http.c    |   39 +++++++++++++++++++++++++++++++++++++++
 src/http.h    |    2 ++
 3 files changed, 42 insertions(+), 0 deletions(-)

diff --git a/src/gateway.c b/src/gateway.c
index b3fd559..88d13e1 100644
--- a/src/gateway.c
+++ b/src/gateway.c
@@ -411,6 +411,7 @@ main_loop(void)
 	httpdAddCContent(webserver, "/wifidog", "about", 0, NULL, http_callback_about);
 	httpdAddCContent(webserver, "/wifidog", "status", 0, NULL, http_callback_status);
 	httpdAddCContent(webserver, "/wifidog", "auth", 0, NULL, http_callback_auth);
+	httpdAddCContent(webserver, "/wifidog", "disconnect", 0, NULL, http_callback_disconnect);
 
 	httpdAddC404Content(webserver, http_callback_404);
 
diff --git a/src/http.c b/src/http.c
index b2b8ff9..d10c107 100644
--- a/src/http.c
+++ b/src/http.c
@@ -276,6 +276,45 @@ http_callback_auth(httpd *webserver, request *r)
 	}
 }
 
+void 
+http_callback_disconnect(httpd *webserver, request *r)
+{
+	/* XXX How do you change the status code for the response?? */
+	httpVar	*token	= httpdGetVariableByName(r, "token");
+	httpVar	*mac	= httpdGetVariableByName(r, "mac");
+
+	if (token && mac) {
+		t_client *client;
+		
+		LOCK_CLIENT_LIST();
+		client = client_list_find_by_mac(mac->value);
+
+		if (!client || strcmp(client->token, token->value)) {
+			UNLOCK_CLIENT_LIST();
+			debug(LOG_INFO, "Disconnect %s with incorrect token %s", mac->value, token->value);
+			httpdOutput(r, "Invalid token for MAC");
+			return -1;
+		}
+
+		/* TODO: get current firewall counters, set counters to auth server,
+		 * send disconnect to auth server.
+		 *
+		 * XXX: this should share code with wdctl_reset
+		 */
+		fw_deny(client->ip, client->mac, client->fw_connection_state);
+		client_list_delete(client);
+
+		UNLOCK_CLIENT_LIST();
+
+	} else {
+		debug(LOG_INFO, "Disconnect called without both token and MAC given");
+		httpdOutput(r, "Both the token and MAC need to be specified"); 
+		return -1;
+	}
+
+	return 0;
+}
+
 void send_http_page(request *r, const char *title, const char* message)
 {
     s_config	*config = config_get_config();
diff --git a/src/http.h b/src/http.h
index 6a3e9bb..6e305f8 100644
--- a/src/http.h
+++ b/src/http.h
@@ -39,6 +39,8 @@ void http_callback_about(httpd *webserver, request *r);
 void http_callback_status(httpd *webserver, request *r);
 /**@brief Callback for libhttpd, main entry point post login for auth confirmation */
 void http_callback_auth(httpd *webserver, request *r);
+/**@brief Callback for libhttpd, disconnect user from network */
+void http_callback_disconnect(httpd *webserver, request *r);
 
 /** @brief Sends a HTML page to web browser */
 void send_http_page(request *r, const char *title, const char* message);
-- 
1.5.5.1


