wifi: Set AP ciphers to CCMP only
[framework/connectivity/connman.git] / plugins / portal.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <stdlib.h>
28
29 #include <glib.h>
30
31 #define CONNMAN_API_SUBJECT_TO_CHANGE
32 #include <connman/plugin.h>
33 #include <connman/location.h>
34 #include <connman/proxy.h>
35 #include <connman/log.h>
36
37 #include "gweb/gweb.h"
38
39 #define STATUS_URL  "http://www.connman.net/online/status.html"
40
41 struct server_data {
42         GWeb *web;
43         guint request_id;
44 };
45
46 static void web_debug(const char *str, void *data)
47 {
48         connman_info("%s: %s\n", (const char *) data, str);
49 }
50
51 static gboolean web_result(GWebResult *result, gpointer user_data)
52 {
53         struct connman_location *location = user_data;
54         struct server_data *data = connman_location_get_data(location);
55         const char *str;
56         guint16 status;
57
58         if (data->request_id == 0)
59                 return FALSE;
60
61         status = g_web_result_get_status(result);
62
63         /* If status header is not available, it is a portal */
64         if (g_web_result_get_header(result, "X-ConnMan-Status", &str) == FALSE)
65                 status = 302;
66
67         DBG("status %u", status);
68
69         switch (status) {
70         case 200:
71                 if (g_web_result_get_header(result, "X-ConnMan-Client-IP",
72                                                                 &str) == TRUE)
73                         connman_info("Client-IP: %s", str);
74
75                 if (g_web_result_get_header(result, "X-ConnMan-Client-Country",
76                                                                 &str) == TRUE)
77                         connman_info("Client-Country: %s", str);
78
79                 connman_location_report_result(location,
80                                         CONNMAN_LOCATION_RESULT_ONLINE);
81                 break;
82         case 302:
83                 connman_location_report_result(location,
84                                         CONNMAN_LOCATION_RESULT_PORTAL);
85                 break;
86         default:
87                 connman_location_report_result(location,
88                                         CONNMAN_LOCATION_RESULT_UNKNOWN);
89                 break;
90         }
91
92         data->request_id = 0;
93
94         return FALSE;
95 }
96
97 static void proxy_callback(const char *proxy, void *user_data)
98 {
99         struct connman_location *location = user_data;
100         struct server_data *data = connman_location_get_data(location);
101
102         DBG("proxy %s", proxy);
103
104         if (proxy == NULL)
105                 proxy = getenv("http_proxy");
106
107         if (data != NULL) {
108                 g_web_set_proxy(data->web, proxy);
109
110                 data->request_id = g_web_request_get(data->web, STATUS_URL,
111                                                         web_result, location);
112         }
113
114         connman_location_unref(location);
115 }
116
117 static int location_detect(struct connman_location *location)
118 {
119         struct server_data *data;
120         enum connman_service_type service_type;
121         char *interface;
122         int err;
123
124         DBG("location %p", location);
125
126         service_type = connman_location_get_type(location);
127
128         switch (service_type) {
129         case CONNMAN_SERVICE_TYPE_ETHERNET:
130         case CONNMAN_SERVICE_TYPE_WIFI:
131         case CONNMAN_SERVICE_TYPE_WIMAX:
132         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
133         case CONNMAN_SERVICE_TYPE_CELLULAR:
134                 break;
135         case CONNMAN_SERVICE_TYPE_UNKNOWN:
136         case CONNMAN_SERVICE_TYPE_SYSTEM:
137         case CONNMAN_SERVICE_TYPE_GPS:
138         case CONNMAN_SERVICE_TYPE_VPN:
139         case CONNMAN_SERVICE_TYPE_GADGET:
140                 return -EOPNOTSUPP;
141         }
142
143         interface = connman_location_get_interface(location);
144         if (interface == NULL)
145                 return -EINVAL;
146
147         DBG("interface %s", interface);
148
149         data = g_try_new0(struct server_data, 1);
150         if (data == NULL) {
151                 err = -ENOMEM;
152                 goto done;
153         }
154
155         connman_location_set_data(location, data);
156
157         data->web = g_web_new(0);
158         if (data->web == NULL) {
159                 g_free(data);
160                 err = -ENOMEM;
161                 goto done;
162         }
163
164         if (getenv("CONNMAN_WEB_DEBUG"))
165                 g_web_set_debug(data->web, web_debug, "WEB");
166
167         g_web_set_accept(data->web, NULL);
168         g_web_set_user_agent(data->web, "ConnMan/%s", VERSION);
169         g_web_set_close_connection(data->web, TRUE);
170
171         err = connman_proxy_lookup(interface, STATUS_URL,
172                                         proxy_callback, location);
173         if (err < 0)
174                 goto done;
175
176         connman_location_ref(location);
177         err = 0;
178
179 done:
180         g_free(interface);
181         return err;
182 }
183
184 static int location_finish(struct connman_location *location)
185 {
186         struct server_data *data = connman_location_get_data(location);
187
188         DBG("location %p", location);
189
190         connman_location_set_data(location, NULL);
191
192         if (data->request_id > 0)
193                 g_web_cancel_request(data->web, data->request_id);
194
195         g_web_unref(data->web);
196
197         g_free(data);
198
199         return 0;
200 }
201
202 static struct connman_location_driver location = {
203         .name           = "portal",
204         .type           = CONNMAN_SERVICE_TYPE_WIFI,
205         .priority       = CONNMAN_LOCATION_PRIORITY_HIGH,
206         .detect         = location_detect,
207         .finish         = location_finish,
208 };
209
210 static int portal_init(void)
211 {
212         return connman_location_driver_register(&location);
213 }
214
215 static void portal_exit(void)
216 {
217         connman_location_driver_unregister(&location);
218 }
219
220 CONNMAN_PLUGIN_DEFINE(portal, "Portal detection plugin", VERSION,
221                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, portal_init, portal_exit)