| 1 | /********************************************************************\ |
|---|
| 2 | * This program is free software; you can redistribute it and/or * |
|---|
| 3 | * modify it under the terms of the GNU General Public License as * |
|---|
| 4 | * published by the Free Software Foundation; either version 2 of * |
|---|
| 5 | * the License, or (at your option) any later version. * |
|---|
| 6 | * * |
|---|
| 7 | * This program is distributed in the hope that it will be useful, * |
|---|
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
|---|
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
|---|
| 10 | * GNU General Public License for more details. * |
|---|
| 11 | * * |
|---|
| 12 | * You should have received a copy of the GNU General Public License* |
|---|
| 13 | * along with this program; if not, contact: * |
|---|
| 14 | * * |
|---|
| 15 | * Free Software Foundation Voice: +1-617-542-5942 * |
|---|
| 16 | * 59 Temple Place - Suite 330 Fax: +1-617-542-2652 * |
|---|
| 17 | * Boston, MA 02111-1307, USA gnu@gnu.org * |
|---|
| 18 | * * |
|---|
| 19 | \********************************************************************/ |
|---|
| 20 | |
|---|
| 21 | /* $Header$ */ |
|---|
| 22 | /** @file http.c |
|---|
| 23 | @brief HTTP IO functions |
|---|
| 24 | @author Copyright (C) 2004 Philippe April <papril777@yahoo.com> |
|---|
| 25 | */ |
|---|
| 26 | |
|---|
| 27 | #define _GNU_SOURCE |
|---|
| 28 | |
|---|
| 29 | #include <stdio.h> |
|---|
| 30 | #include <stdlib.h> |
|---|
| 31 | #include <pthread.h> |
|---|
| 32 | #include <string.h> |
|---|
| 33 | #include <unistd.h> |
|---|
| 34 | #include <syslog.h> |
|---|
| 35 | |
|---|
| 36 | #include "httpd.h" |
|---|
| 37 | |
|---|
| 38 | #include "debug.h" |
|---|
| 39 | #include "conf.h" |
|---|
| 40 | #include "auth.h" |
|---|
| 41 | #include "firewall.h" |
|---|
| 42 | #include "http.h" |
|---|
| 43 | #include "httpd.h" |
|---|
| 44 | #include "client_list.h" |
|---|
| 45 | #include "common.h" |
|---|
| 46 | |
|---|
| 47 | #include "util.h" |
|---|
| 48 | |
|---|
| 49 | #include "../config.h" |
|---|
| 50 | |
|---|
| 51 | extern pthread_mutex_t client_list_mutex; |
|---|
| 52 | |
|---|
| 53 | void |
|---|
| 54 | http_callback_404(httpd *webserver, request *r) |
|---|
| 55 | { |
|---|
| 56 | char *newlocation, |
|---|
| 57 | protocol[6], |
|---|
| 58 | tmp_url[MAX_BUF], |
|---|
| 59 | *url; |
|---|
| 60 | int port; |
|---|
| 61 | s_config *config = config_get_config(); |
|---|
| 62 | t_auth_serv *auth_server = get_auth_server(); |
|---|
| 63 | |
|---|
| 64 | if (auth_server->authserv_use_ssl) { |
|---|
| 65 | strcpy(protocol, "https"); |
|---|
| 66 | port = auth_server->authserv_ssl_port; |
|---|
| 67 | } else { |
|---|
| 68 | strcpy(protocol, "http"); |
|---|
| 69 | port = auth_server->authserv_http_port; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | memset(tmp_url, 0, sizeof(tmp_url)); |
|---|
| 73 | snprintf(tmp_url, (sizeof(tmp_url) - 1), "http://%s%s", |
|---|
| 74 | r->request.host, |
|---|
| 75 | r->request.path); |
|---|
| 76 | url = httpdUrlEncode(tmp_url); |
|---|
| 77 | |
|---|
| 78 | if (!is_online()) { |
|---|
| 79 | /* No point re-directing them to the auth server if we haven't been able to talk |
|---|
| 80 | * to it for a while */ |
|---|
| 81 | httpdOutput(r, "<html><head><title>Currently unavailable</title></head><body><h1>Uh oh!</h1>"); |
|---|
| 82 | httpdOutput(r, "We apologize, but it seems that we are currently unable to re-direct you to the login screen."); |
|---|
| 83 | httpdOutput(r, "<p>"); |
|---|
| 84 | httpdOutput(r, "The maintainers of this network are aware of this disruption. We hope that this situation will be resolved soon."); |
|---|
| 85 | httpdOutput(r, "<p>"); |
|---|
| 86 | httpdPrintf(r, "In a while please <a href='%s'>click here</a> to try again.", url); |
|---|
| 87 | httpdOutput(r, "</body></html>"); |
|---|
| 88 | debug(LOG_INFO, "Sent %s an apology since I am not online - no point sending them to auth server", r->clientAddr); |
|---|
| 89 | } |
|---|
| 90 | else if ((asprintf(&newlocation, "Location: %s://%s:%d%slogin?" |
|---|
| 91 | "gw_address=%s&gw_port=%d&gw_id=%s&url=%s", |
|---|
| 92 | protocol, |
|---|
| 93 | auth_server->authserv_hostname, |
|---|
| 94 | port, |
|---|
| 95 | auth_server->authserv_path, |
|---|
| 96 | config->gw_address, config->gw_port, |
|---|
| 97 | config->gw_id, |
|---|
| 98 | url)) == -1) { |
|---|
| 99 | debug(LOG_ERR, "Failed to asprintf newlocation"); |
|---|
| 100 | httpdOutput(r, "Internal error occurred"); |
|---|
| 101 | } else { |
|---|
| 102 | /* Re-direct them to auth server */ |
|---|
| 103 | httpdSetResponse(r, "307 Please authenticate yourself here\n"); |
|---|
| 104 | httpdAddHeader(r, newlocation); |
|---|
| 105 | httpdPrintf(r, "<html><head><title>Redirection</title></head><body>" |
|---|
| 106 | "Please <a href='%s://%s:%d%slogin?gw_address" |
|---|
| 107 | "=%s&gw_port=%d&gw_id=%s&url=%s'>click here</a> to " |
|---|
| 108 | "login", |
|---|
| 109 | protocol, |
|---|
| 110 | auth_server->authserv_hostname, |
|---|
| 111 | port, |
|---|
| 112 | auth_server->authserv_path, |
|---|
| 113 | config->gw_address, |
|---|
| 114 | config->gw_port, |
|---|
| 115 | config->gw_id, |
|---|
| 116 | url); |
|---|
| 117 | debug(LOG_INFO, "Captured %s and re-directed them to login " |
|---|
| 118 | "page", r->clientAddr); |
|---|
| 119 | free(newlocation); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | free(url); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | void |
|---|
| 126 | http_callback_about(httpd *webserver, request *r) |
|---|
| 127 | { |
|---|
| 128 | httpdOutput(r, "<html><head><title>About WiFiDog</title></head><body><h1>About WiFiDog:</h1>"); |
|---|
| 129 | httpdOutput(r, "This is WiFiDog version <b>" VERSION "</b>"); |
|---|
| 130 | httpdOutput(r, "<p>"); |
|---|
| 131 | httpdOutput(r, "Copyright (C) 2004-2005. This software is released under the GNU GPL license."); |
|---|
| 132 | httpdOutput(r, "<p>"); |
|---|
| 133 | httpdOutput(r, "For more information visit <a href='http://" |
|---|
| 134 | "www.ilesansfil.org/wiki/WiFiDog'>http://www." |
|---|
| 135 | "ilesansfil.org/wiki/WiFiDog</a>"); |
|---|
| 136 | httpdOutput(r, "</body></html>"); |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | void |
|---|
| 140 | http_callback_auth(httpd *webserver, request *r) |
|---|
| 141 | { |
|---|
| 142 | t_client *client; |
|---|
| 143 | httpVar * token; |
|---|
| 144 | char *mac; |
|---|
| 145 | |
|---|
| 146 | if ((token = httpdGetVariableByName(r, "token"))) { |
|---|
| 147 | /* They supplied variable "token" */ |
|---|
| 148 | if (!(mac = arp_get(r->clientAddr))) { |
|---|
| 149 | /* We could not get their MAC address */ |
|---|
| 150 | debug(LOG_ERR, "Failed to retrieve MAC address for " |
|---|
| 151 | "ip %s", r->clientAddr); |
|---|
| 152 | httpdOutput(r, "Failed to retrieve your MAC " |
|---|
| 153 | "address"); |
|---|
| 154 | } else { |
|---|
| 155 | /* We have their MAC address */ |
|---|
| 156 | |
|---|
| 157 | LOCK_CLIENT_LIST(); |
|---|
| 158 | |
|---|
| 159 | if ((client = client_list_find(r->clientAddr, mac)) == NULL) { |
|---|
| 160 | debug(LOG_DEBUG, "New client for %s", |
|---|
| 161 | r->clientAddr); |
|---|
| 162 | client_list_append(r->clientAddr, mac, token->value); |
|---|
| 163 | } else { |
|---|
| 164 | debug(LOG_DEBUG, "Node for %s already " |
|---|
| 165 | "exists", client->ip); |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | UNLOCK_CLIENT_LIST(); |
|---|
| 169 | |
|---|
| 170 | authenticate_client(r); |
|---|
| 171 | free(mac); |
|---|
| 172 | } |
|---|
| 173 | } else { |
|---|
| 174 | /* They did not supply variable "token" */ |
|---|
| 175 | httpdOutput(r, "Invalid token"); |
|---|
| 176 | } |
|---|
| 177 | } |
|---|