root/trunk/wifidog/src/http.c @ 186

Revision 186, 5.0 KB (checked in by alexcv, 9 years ago)

Changed config for AuthServer?

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