| 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 "debug.h" |
|---|
| 37 | #include "conf.h" |
|---|
| 38 | #include "auth.h" |
|---|
| 39 | #include "firewall.h" |
|---|
| 40 | #include "http.h" |
|---|
| 41 | #include "httpd.h" |
|---|
| 42 | #include "client_list.h" |
|---|
| 43 | #include "common.h" |
|---|
| 44 | |
|---|
| 45 | extern pthread_mutex_t client_list_mutex; |
|---|
| 46 | |
|---|
| 47 | void |
|---|
| 48 | http_callback_404(httpd * webserver) |
|---|
| 49 | { |
|---|
| 50 | char *newlocation, |
|---|
| 51 | protocol[6], |
|---|
| 52 | tmp_url[MAX_BUF], |
|---|
| 53 | *url; |
|---|
| 54 | int port; |
|---|
| 55 | s_config *config = config_get_config(); |
|---|
| 56 | t_auth_serv *auth_server = get_auth_server(); |
|---|
| 57 | |
|---|
| 58 | if (auth_server->authserv_use_ssl) { |
|---|
| 59 | strcpy(protocol, "https"); |
|---|
| 60 | port = auth_server->authserv_ssl_port; |
|---|
| 61 | } else { |
|---|
| 62 | strcpy(protocol, "http"); |
|---|
| 63 | port = auth_server->authserv_http_port; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | memset(tmp_url, 0, sizeof(tmp_url)); |
|---|
| 67 | snprintf(tmp_url, (sizeof(tmp_url) - 1), "http://%s%s", |
|---|
| 68 | webserver->request.host, |
|---|
| 69 | webserver->request.path); |
|---|
| 70 | url = httpdUrlEncode(tmp_url); |
|---|
| 71 | |
|---|
| 72 | if ((asprintf(&newlocation, "Location: %s://%s:%d%s/login?" |
|---|
| 73 | "gw_address=%s&gw_port=%d&gw_id=%s&url=%s", |
|---|
| 74 | protocol, |
|---|
| 75 | auth_server->authserv_hostname, |
|---|
| 76 | port, |
|---|
| 77 | auth_server->authserv_path, |
|---|
| 78 | config->gw_address, config->gw_port, |
|---|
| 79 | config->gw_id, |
|---|
| 80 | url)) == -1) { |
|---|
| 81 | debug(LOG_ERR, "Failed to asprintf newlocation"); |
|---|
| 82 | free(url); |
|---|
| 83 | free(newlocation); |
|---|
| 84 | httpdOutput(webserver, "Internal error occurred"); |
|---|
| 85 | } else { |
|---|
| 86 | /* Re-direct them to auth server */ |
|---|
| 87 | httpdSetResponse(webserver, "307 Please authenticate yourself here"); |
|---|
| 88 | httpdAddHeader(webserver, newlocation); |
|---|
| 89 | httpdPrintf(webserver, "<html><head><title>Redirection</title></head><body>" |
|---|
| 90 | "Please <a href='%s://%s:%d%s/login?gw_address" |
|---|
| 91 | "=%s&gw_port=%d&gw_id=%s&url=%s'>click here</a> to " |
|---|
| 92 | "login", |
|---|
| 93 | protocol, |
|---|
| 94 | auth_server->authserv_hostname, |
|---|
| 95 | port, |
|---|
| 96 | auth_server->authserv_path, |
|---|
| 97 | config->gw_address, |
|---|
| 98 | config->gw_port, |
|---|
| 99 | config->gw_id, |
|---|
| 100 | url); |
|---|
| 101 | debug(LOG_INFO, "Captured %s and re-directed them to login " |
|---|
| 102 | "page", webserver->clientAddr); |
|---|
| 103 | free(url); |
|---|
| 104 | free(newlocation); |
|---|
| 105 | } |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | void |
|---|
| 109 | http_callback_about(httpd * webserver) |
|---|
| 110 | { |
|---|
| 111 | httpdOutput(webserver, "<html><body><h1>About:</h1>"); |
|---|
| 112 | httpdOutput(webserver, "This is WiFiDog. Copyright (C) 2004 and " |
|---|
| 113 | "released under the GNU GPL license."); |
|---|
| 114 | httpdOutput(webserver, "<p>"); |
|---|
| 115 | httpdOutput(webserver, "For more information visit <a href='http://" |
|---|
| 116 | "www.ilesansfil.org/wiki/WiFiDog'>http://www." |
|---|
| 117 | "ilesansfil.org/wiki/WiFiDog</a>"); |
|---|
| 118 | httpdOutput(webserver, "</body></html>"); |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | void |
|---|
| 122 | http_callback_auth(httpd * webserver) |
|---|
| 123 | { |
|---|
| 124 | t_client *client; |
|---|
| 125 | httpVar * token; |
|---|
| 126 | char *mac, |
|---|
| 127 | *ip; |
|---|
| 128 | pthread_t tid; |
|---|
| 129 | |
|---|
| 130 | if ((token = httpdGetVariableByName(webserver, "token"))) { |
|---|
| 131 | /* They supplied variable "token" */ |
|---|
| 132 | if (!(mac = arp_get(webserver->clientAddr))) { |
|---|
| 133 | /* We could not get their MAC address */ |
|---|
| 134 | debug(LOG_ERR, "Failed to retrieve MAC address for " |
|---|
| 135 | "ip %s", webserver->clientAddr); |
|---|
| 136 | httpdOutput(webserver, "Failed to retrieve your MAC " |
|---|
| 137 | "address"); |
|---|
| 138 | } else { |
|---|
| 139 | /* We have their MAC address */ |
|---|
| 140 | |
|---|
| 141 | pthread_mutex_lock(&client_list_mutex); |
|---|
| 142 | |
|---|
| 143 | if ((client = client_list_find(webserver->clientAddr, mac)) == NULL) { |
|---|
| 144 | debug(LOG_DEBUG, "New client for %s", |
|---|
| 145 | webserver->clientAddr); |
|---|
| 146 | client_list_append(webserver->clientAddr, mac, token->value); |
|---|
| 147 | } else { |
|---|
| 148 | debug(LOG_DEBUG, "Node for %s already " |
|---|
| 149 | "exists", client->ip); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | client = client_list_find(webserver->clientAddr, mac); |
|---|
| 153 | |
|---|
| 154 | client->fd = webserver->clientSock; |
|---|
| 155 | webserver->clientSock = -1; |
|---|
| 156 | |
|---|
| 157 | pthread_mutex_unlock(&client_list_mutex); |
|---|
| 158 | |
|---|
| 159 | /* That clientAddr may be freed prior to the thread |
|---|
| 160 | * finishing. XXX The duplicated string will be freed |
|---|
| 161 | * by the thread */ |
|---|
| 162 | ip = strdup(webserver->clientAddr); |
|---|
| 163 | |
|---|
| 164 | /* start sub process */ |
|---|
| 165 | pthread_create(&tid, NULL, (void *)thread_authenticate_client, (void *)ip); |
|---|
| 166 | pthread_detach(tid); |
|---|
| 167 | |
|---|
| 168 | free(mac); |
|---|
| 169 | } |
|---|
| 170 | } else { |
|---|
| 171 | /* They did not supply variable "token" */ |
|---|
| 172 | httpdOutput(webserver, "Invalid token"); |
|---|
| 173 | } |
|---|
| 174 | } |
|---|