Main Page | Data Structures | Directories | File List | Data Fields

wifidog-1.1.3_beta2/src/http.c

00001 /********************************************************************\
00002  * This program is free software; you can redistribute it and/or    *
00003  * modify it under the terms of the GNU General Public License as   *
00004  * published by the Free Software Foundation; either version 2 of   *
00005  * the License, or (at your option) any later version.              *
00006  *                                                                  *
00007  * This program is distributed in the hope that it will be useful,  *
00008  * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
00009  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
00010  * GNU General Public License for more details.                     *
00011  *                                                                  *
00012  * You should have received a copy of the GNU General Public License*
00013  * along with this program; if not, contact:                        *
00014  *                                                                  *
00015  * Free Software Foundation           Voice:  +1-617-542-5942       *
00016  * 59 Temple Place - Suite 330        Fax:    +1-617-542-2652       *
00017  * Boston, MA  02111-1307,  USA       gnu@gnu.org                   *
00018  *                                                                  *
00019  \********************************************************************/
00020 
00021 /* $Id: http.c 935 2006-01-31 22:22:04 -0500 (Tue, 31 Jan 2006) benoitg $ */
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 
00055 void
00056 http_callback_404(httpd *webserver, request *r)
00057 {
00058         char            *newlocation,
00059                         *protocol,
00060                         tmp_url[MAX_BUF],
00061                         *url;
00062         int             port;
00063         s_config        *config = config_get_config();
00064         t_auth_serv     *auth_server = get_auth_server();
00065 
00066         if (auth_server->authserv_use_ssl) {
00067                 protocol = "https";
00068                 port = auth_server->authserv_ssl_port;
00069         } else {
00070                 protocol = "http";
00071                 port = auth_server->authserv_http_port;
00072         }
00073 
00074         memset(tmp_url, 0, sizeof(tmp_url));
00075         snprintf(tmp_url, (sizeof(tmp_url) - 1), "http://%s%s",
00076                         r->request.host,
00077                         r->request.path);
00078         url = httpdUrlEncode(tmp_url);
00079 
00080         if (!is_online()) {
00081                 /* The internet connection is down at the moment  - apologize and do not redirect anywhere */
00082                 http_wifidog_header(r, "<h2>Uh oh! Internet access unavailable</h2>");
00083                 httpdOutput(r, "<p>We apologize, but it seems that the internet connection that powers this hotspot is temporarily unavailable.</p>");
00084                 httpdOutput(r, "<p>If at all possible, please notify the owners of this hotspot that the internet connection is out of service.</p>");
00085                 httpdOutput(r, "<p>The maintainers of this network are aware of this disruption.  We hope that this situation will be resolved soon.</p>");
00086                 httpdPrintf(r, "<p>In a while please <a href='%s'>click here</a> to try your request again.</p>", tmp_url);
00087                 http_wifidog_footer(r);
00088                 debug(LOG_INFO, "Sent %s an apology since I am not online - no point sending them to auth server", r->clientAddr);
00089         }
00090         else if (!is_auth_online()) {
00091                 /* The auth server is down at the moment - apologize and do not redirect anywhere */
00092                 http_wifidog_header(r, "<h2>Uh oh! Login screen unavailable</h2>");
00093                 httpdOutput(r, "<p>We apologize, but it seems that we are currently unable to re-direct you to the login screen.</p>");
00094                 httpdOutput(r, "<p>The maintainers of this network are aware of this disruption.  We hope that this situation will be resolved soon.</p>");
00095                 httpdPrintf(r, "<p>In a couple of minutes please <a href='%s'>click here</a> to try your request again.</p>", tmp_url);
00096                 http_wifidog_footer(r);
00097                 debug(LOG_INFO, "Sent %s an apology since auth server not online - no point sending them to auth server", r->clientAddr);
00098         }
00099         else {
00100                 /* Re-direct them to auth server */
00101                 safe_asprintf(&newlocation, "Location: %s://%s:%d%slogin?gw_address=%s&gw_port=%d&gw_id=%s&url=%s",
00102                         protocol,
00103                         auth_server->authserv_hostname,
00104                         port,
00105                         auth_server->authserv_path,
00106                         config->gw_address,
00107                         config->gw_port, 
00108                         config->gw_id,
00109                         url);
00110                 httpdSetResponse(r, "307 Please authenticate yourself here\n");
00111                 httpdAddHeader(r, newlocation);
00112                 http_wifidog_header(r, "Redirection");
00113                 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",
00114                                 protocol,
00115                                 auth_server->authserv_hostname,
00116                                 port,
00117                                 auth_server->authserv_path,
00118                                 config->gw_address, 
00119                                 config->gw_port,
00120                                 config->gw_id,
00121                                 url);
00122                 http_wifidog_footer(r);
00123                 debug(LOG_INFO, "Captured %s requesting [%s] and re-directed them to login page", r->clientAddr, url);
00124                 free(newlocation);
00125         }
00126 
00127         free(url);
00128 }
00129 
00130 void 
00131 http_callback_wifidog(httpd *webserver, request *r)
00132 {
00133         http_wifidog_header(r, "WiFiDog");
00134         httpdOutput(r, "Please use the menu to navigate the features of this WiFiDog installation.");
00135         http_wifidog_footer(r);
00136 }
00137 
00138 void 
00139 http_callback_about(httpd *webserver, request *r)
00140 {
00141         http_wifidog_header(r, "About WiFiDog");
00142         httpdOutput(r, "This is WiFiDog version <b>" VERSION "</b>");
00143         http_wifidog_footer(r);
00144 }
00145 
00146 void 
00147 http_callback_status(httpd *webserver, request *r)
00148 {
00149         char * status = NULL;
00150         status = get_status_text();
00151         http_wifidog_header(r, "WiFiDog Status");
00152         httpdOutput(r, "<pre>");
00153         httpdOutput(r, status);
00154         httpdOutput(r, "</pre>");
00155         http_wifidog_footer(r);
00156         free(status);
00157 }
00158 
00159 void 
00160 http_callback_auth(httpd *webserver, request *r)
00161 {
00162         t_client        *client;
00163         httpVar * token;
00164         char    *mac;
00165 
00166         if ((token = httpdGetVariableByName(r, "token"))) {
00167                 /* They supplied variable "token" */
00168                 if (!(mac = arp_get(r->clientAddr))) {
00169                         /* We could not get their MAC address */
00170                         debug(LOG_ERR, "Failed to retrieve MAC address for ip %s", r->clientAddr);
00171                         http_wifidog_header(r, "WiFiDog Error");
00172                         httpdOutput(r, "Failed to retrieve your MAC address");
00173                         http_wifidog_footer(r);
00174                 } else {
00175                         /* We have their MAC address */
00176 
00177                         LOCK_CLIENT_LIST();
00178                         
00179                         if ((client = client_list_find(r->clientAddr, mac)) == NULL) {
00180                                 debug(LOG_DEBUG, "New client for %s", r->clientAddr);
00181                                 client_list_append(r->clientAddr, mac, token->value);
00182                         } else {
00183                                 debug(LOG_DEBUG, "Node for %s already exists", client->ip);
00184                         }
00185 
00186                         UNLOCK_CLIENT_LIST();
00187 
00188                         authenticate_client(r);
00189                         free(mac);
00190                 }
00191         } else {
00192                 /* They did not supply variable "token" */
00193                 http_wifidog_header(r, "WiFiDog Error");
00194                 httpdOutput(r, "Invalid token");
00195                 http_wifidog_footer(r);
00196         }
00197 }
00198 
00199 void
00200 http_wifidog_header(request *r, char *title)
00201 {
00202     httpdOutput(r, "<html>\n");
00203     httpdOutput(r, "<head>\n");
00204     httpdPrintf(r, "<title>%s</title>\n", title);
00205     httpdOutput(r, "<meta HTTP-EQUIV='Pragma' CONTENT='no-cache'>\n");
00206 
00207     httpdOutput(r, "<style>\n");
00208     httpdOutput(r, "body {\n");
00209     httpdOutput(r, "  margin: 10px 60px 0 60px; \n");
00210     httpdOutput(r, "  font-family : bitstream vera sans, sans-serif;\n");
00211     httpdOutput(r, "  color: #46a43a;\n");
00212     httpdOutput(r, "}\n");
00213 
00214     httpdOutput(r, "a {\n");
00215     httpdOutput(r, "  color: #46a43a;\n");
00216     httpdOutput(r, "}\n");
00217 
00218     httpdOutput(r, "a:active {\n");
00219     httpdOutput(r, "  color: #46a43a;\n");
00220     httpdOutput(r, "}\n");
00221 
00222     httpdOutput(r, "a:link {\n");
00223     httpdOutput(r, "  color: #46a43a;\n");
00224     httpdOutput(r, "}\n");
00225 
00226     httpdOutput(r, "a:visited {\n");
00227     httpdOutput(r, "  color: #46a43a;\n");
00228     httpdOutput(r, "}\n");
00229 
00230     httpdOutput(r, "#header {\n");
00231     httpdOutput(r, "  height: 30px;\n");
00232     httpdOutput(r, "  background-color: #B4F663;\n");
00233     httpdOutput(r, "  padding: 20px;\n");
00234     httpdOutput(r, "  font-size: 20pt;\n");
00235     httpdOutput(r, "  text-align: center;\n");
00236     httpdOutput(r, "  border: 2px solid #46a43a;\n");
00237     httpdOutput(r, "  border-bottom: 0;\n");
00238     httpdOutput(r, "}\n");
00239 
00240     httpdOutput(r, "#menu {\n");
00241     httpdOutput(r, "  width: 200px;\n");
00242     httpdOutput(r, "  float: right;\n");
00243     httpdOutput(r, "  background-color: #B4F663;\n");
00244     httpdOutput(r, "  border: 2px solid #46a43a;\n");
00245     httpdOutput(r, "  font-size: 80%;\n");
00246     httpdOutput(r, "  min-height: 300px;\n");
00247     httpdOutput(r, "}\n");
00248 
00249     httpdOutput(r, "#menu h2 {\n");
00250     httpdOutput(r, "  margin: 0;\n");
00251     httpdOutput(r, "  background-color: #46a43a;\n");
00252     httpdOutput(r, "  text-align: center;\n");
00253     httpdOutput(r, "  color: #B4F663;\n");
00254     httpdOutput(r, "}\n");
00255 
00256     httpdOutput(r, "#copyright {\n");
00257     httpdOutput(r, "}\n");
00258 
00259     httpdOutput(r, "#content {\n");
00260     httpdOutput(r, "  padding: 20px;\n");
00261     httpdOutput(r, "  border: 2px solid #46a43a;\n");
00262     httpdOutput(r, "  min-height: 300px;\n");
00263     httpdOutput(r, "}\n");
00264     httpdOutput(r, "</style>\n");
00265 
00266     httpdOutput(r, "</head>\n");
00267 
00268     httpdOutput(r, "<body\n");
00269 
00270     httpdOutput(r, "<div id=\"header\">\n");
00271     httpdPrintf(r, "    %s\n", title);
00272     httpdOutput(r, "</div>\n");
00273 
00274     httpdOutput(r, "<div id=\"menu\">\n");
00275 
00276 
00277     httpdOutput(r, "    <h2>Info</h2>\n");
00278     httpdOutput(r, "    <ul>\n");
00279     httpdOutput(r, "    <li>Version: " VERSION "\n");
00280     httpdPrintf(r, "    <li>Node ID: %s\n", config_get_config()->gw_id);
00281     httpdOutput(r, "    </ul>\n");
00282     httpdOutput(r, "    <br>\n");
00283 
00284     httpdOutput(r, "    <h2>Menu</h2>\n");
00285     httpdOutput(r, "    <ul>\n");
00286     httpdOutput(r, "    <li><a href='/wifidog/status'>WiFiDog Status</a>\n");
00287     httpdOutput(r, "    <li><a href='/wifidog/about'>About WiFiDog</a>\n");
00288     httpdOutput(r, "    <li><a href='http://www.wifidog.org'>WiFiDog's homepage</a>\n");
00289     httpdOutput(r, "    </ul>\n");
00290     httpdOutput(r, "</div>\n");
00291 
00292     httpdOutput(r, "<div id=\"content\">\n");
00293     httpdPrintf(r, "<h2>%s</h2>\n", title);
00294 }
00295 
00296 void
00297 http_wifidog_footer(request *r)
00298 {
00299         httpdOutput(r, "</div>\n");
00300 
00301     httpdOutput(r, "<div id=\"copyright\">\n");
00302     httpdOutput(r, "Copyright (C) 2004-2005.  This software is released under the GNU GPL license.\n");
00303     httpdOutput(r, "</div>\n");
00304 
00305 
00306         httpdOutput(r, "</body>\n");
00307         httpdOutput(r, "</html>\n");
00308 }

Generated on Tue Jan 31 23:13:16 2006 for WifiDog by  doxygen 1.4.4