00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #define _GNU_SOURCE
00028
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 #include <pthread.h>
00032 #include <string.h>
00033 #include <unistd.h>
00034 #include <syslog.h>
00035
00036 #include "httpd.h"
00037
00038 #include "safe.h"
00039 #include "debug.h"
00040 #include "conf.h"
00041 #include "auth.h"
00042 #include "firewall.h"
00043 #include "http.h"
00044 #include "httpd.h"
00045 #include "client_list.h"
00046 #include "common.h"
00047
00048 #include "util.h"
00049
00050 #include "../config.h"
00051
00052 extern pthread_mutex_t client_list_mutex;
00053
00054 void
00055 http_callback_404(httpd *webserver, request *r)
00056 {
00057 char *newlocation,
00058 *protocol,
00059 tmp_url[MAX_BUF],
00060 *url;
00061 int port;
00062 s_config *config = config_get_config();
00063 t_auth_serv *auth_server = get_auth_server();
00064
00065 if (auth_server->authserv_use_ssl) {
00066 protocol = "https";
00067 port = auth_server->authserv_ssl_port;
00068 } else {
00069 protocol = "http";
00070 port = auth_server->authserv_http_port;
00071 }
00072
00073 memset(tmp_url, 0, sizeof(tmp_url));
00074 snprintf(tmp_url, (sizeof(tmp_url) - 1), "http://%s%s",
00075 r->request.host,
00076 r->request.path);
00077 url = httpdUrlEncode(tmp_url);
00078
00079 if (!is_online()) {
00080
00081 http_wifidog_header(r, "Uh oh! Internet access unavailable");
00082 httpdOutput(r, "We apologize, but it seems that the internet connection that powers this hotspot is temporarily unavailable.");
00083 httpdOutput(r, "<p>");
00084 httpdOutput(r, "If at all possible, please notify the owners of this hotspot that the internet connection is out of service.");
00085 httpdOutput(r, "<p>");
00086 httpdOutput(r, "The maintainers of this network are aware of this disruption. We hope that this situation will be resolved soon.");
00087 httpdOutput(r, "<p>");
00088 httpdPrintf(r, "In a while please <a href='%s'>click here</a> to try your request again.", tmp_url);
00089 http_wifidog_footer(r);
00090 debug(LOG_INFO, "Sent %s an apology since I am not online - no point sending them to auth server", r->clientAddr);
00091 }
00092 else if (!is_auth_online()) {
00093
00094 http_wifidog_header(r, "Uh oh! Login screen unavailable");
00095 httpdOutput(r, "We apologize, but it seems that we are currently unable to re-direct you to the login screen.");
00096 httpdOutput(r, "<p>");
00097 httpdOutput(r, "The maintainers of this network are aware of this disruption. We hope that this situation will be resolved soon.");
00098 httpdOutput(r, "<p>");
00099 httpdPrintf(r, "In a couple of minutes please <a href='%s'>click here</a> to try your request again.", tmp_url);
00100 http_wifidog_footer(r);
00101 debug(LOG_INFO, "Sent %s an apology since auth server not online - no point sending them to auth server", r->clientAddr);
00102 }
00103 else {
00104
00105 safe_asprintf(&newlocation, "Location: %s://%s:%d%slogin?gw_address=%s&gw_port=%d&gw_id=%s&url=%s",
00106 protocol,
00107 auth_server->authserv_hostname,
00108 port,
00109 auth_server->authserv_path,
00110 config->gw_address,
00111 config->gw_port,
00112 config->gw_id,
00113 url);
00114 httpdSetResponse(r, "307 Please authenticate yourself here\n");
00115 httpdAddHeader(r, newlocation);
00116 http_wifidog_header(r, "Redirection");
00117 httpdPrintf(r, "Please <a href='%s://%s:%d%slogin?gw_address=%s&gw_port=%d&gw_id=%s&url=%s'>click here</a> to login",
00118 protocol,
00119 auth_server->authserv_hostname,
00120 port,
00121 auth_server->authserv_path,
00122 config->gw_address,
00123 config->gw_port,
00124 config->gw_id,
00125 url);
00126 http_wifidog_footer(r);
00127 debug(LOG_INFO, "Captured %s requesting [%s] and re-directed them to login page", r->clientAddr, url);
00128 free(newlocation);
00129 }
00130
00131 free(url);
00132 }
00133
00134 void
00135 http_callback_wifidog(httpd *webserver, request *r)
00136 {
00137 http_wifidog_header(r, "WiFiDog");
00138 httpdOutput(r, "Please use the menu on the left to navigate the features of this WiFiDog installation.");
00139 http_wifidog_footer(r);
00140 }
00141
00142 void
00143 http_callback_about(httpd *webserver, request *r)
00144 {
00145 http_wifidog_header(r, "About WiFiDog");
00146 httpdOutput(r, "This is WiFiDog version <b>" VERSION "</b>");
00147 http_wifidog_footer(r);
00148 }
00149
00150 void
00151 http_callback_status(httpd *webserver, request *r)
00152 {
00153 char * status = NULL;
00154 status = get_status_text();
00155 http_wifidog_header(r, "WiFiDog Status");
00156 httpdOutput(r, "<pre>");
00157 httpdOutput(r, status);
00158 httpdOutput(r, "</pre>");
00159 http_wifidog_footer(r);
00160 free(status);
00161 }
00162
00163 void
00164 http_callback_auth(httpd *webserver, request *r)
00165 {
00166 t_client *client;
00167 httpVar * token;
00168 char *mac;
00169
00170 if ((token = httpdGetVariableByName(r, "token"))) {
00171
00172 if (!(mac = arp_get(r->clientAddr))) {
00173
00174 debug(LOG_ERR, "Failed to retrieve MAC address for ip %s", r->clientAddr);
00175 http_wifidog_header(r, "WiFiDog Error");
00176 httpdOutput(r, "Failed to retrieve your MAC address");
00177 http_wifidog_footer(r);
00178 } else {
00179
00180
00181 LOCK_CLIENT_LIST();
00182
00183 if ((client = client_list_find(r->clientAddr, mac)) == NULL) {
00184 debug(LOG_DEBUG, "New client for %s", r->clientAddr);
00185 client_list_append(r->clientAddr, mac, token->value);
00186 } else {
00187 debug(LOG_DEBUG, "Node for %s already exists", client->ip);
00188 }
00189
00190 UNLOCK_CLIENT_LIST();
00191
00192 authenticate_client(r);
00193 free(mac);
00194 }
00195 } else {
00196
00197 http_wifidog_header(r, "WiFiDog Error");
00198 httpdOutput(r, "Invalid token");
00199 http_wifidog_footer(r);
00200 }
00201 }
00202
00203 void
00204 http_wifidog_header(request *r, char *title)
00205 {
00206 httpdOutput(r, "<html>\n");
00207 httpdOutput(r, "<head>\n");
00208 httpdPrintf(r, "<title>%s</title>\n", title);
00209 httpdOutput(r, "<meta HTTP-EQUIV='Pragma' CONTENT='no-cache'>\n");
00210 httpdOutput(r, "</head>\n");
00211 httpdOutput(r, "<body topmargin=0 leftmargin=0 marginwidth=0 marginheight=0 bgcolor=white text=#628C53 link=blue alink=blue vlink=blue>\n");
00212
00213 httpdOutput(r, "<table width=100%% height=100%% border=0 cellpadding=12 cellspacing=5>\n");
00214
00215 httpdOutput(r, "<tr>\n");
00216
00217 httpdOutput(r, "<td valign=top align=right width=30%% bgcolor=#e1f5da>\n");
00218 httpdOutput(r, " <p>\n");
00219 httpdOutput(r, " <p>\n");
00220 httpdOutput(r, "<a href='/wifidog/status'>WiFiDog Status</a>\n");
00221 httpdOutput(r, "<p>\n");
00222 httpdOutput(r, "<a href='/wifidog/about'>About WiFiDog</a>\n");
00223 httpdOutput(r, "<p>\n");
00224 httpdOutput(r, "<a href='http://www.ilesansfil.org/wiki/WiFiDog'>WiFiDog's homepage</a>\n");
00225 httpdOutput(r, "</td>\n");
00226
00227 httpdOutput(r, "<td valign=top align=left>\n");
00228 httpdPrintf(r, "<h1>%s</h1>\n", title);
00229 httpdOutput(r, "<hr>\n");
00230
00231 }
00232
00233 void
00234 http_wifidog_footer(request *r)
00235 {
00236 httpdOutput(r, "</td>\n");
00237
00238 httpdOutput(r, "</tr>\n");
00239
00240 httpdOutput(r, "<tr>\n");
00241
00242 httpdOutput(r, "<td colspan=2 height=1 valign=bottom align=center>\n");
00243 httpdOutput(r, "<hr>\n");
00244 httpdOutput(r, "<font size=1>\n");
00245 httpdOutput(r, "Copyright (C) 2004-2005. This software is released under the GNU GPL license.\n");
00246 httpdOutput(r, "</font>\n");
00247 httpdOutput(r, "</td>\n");
00248
00249 httpdOutput(r, "</tr>\n");
00250
00251 httpdOutput(r, "</table>\n");
00252
00253 httpdOutput(r, "</body>\n");
00254 httpdOutput(r, "</html>\n");
00255 }