Imported Upstream version 1.38
[platform/upstream/connman.git] / src / wispr.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2013  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 <gweb/gweb.h>
30
31 #include "connman.h"
32
33 #define STATUS_URL_IPV4  "http://ipv4.connman.net/online/status.html"
34 #define STATUS_URL_IPV6  "http://ipv6.connman.net/online/status.html"
35
36 struct connman_wispr_message {
37         bool has_error;
38         const char *current_element;
39         int message_type;
40         int response_code;
41         char *login_url;
42         char *abort_login_url;
43         char *logoff_url;
44         char *access_procedure;
45         char *access_location;
46         char *location_name;
47 };
48
49 enum connman_wispr_result {
50         CONNMAN_WISPR_RESULT_UNKNOWN = 0,
51         CONNMAN_WISPR_RESULT_LOGIN   = 1,
52         CONNMAN_WISPR_RESULT_ONLINE  = 2,
53         CONNMAN_WISPR_RESULT_FAILED  = 3,
54 };
55
56 struct wispr_route {
57         char *address;
58         int if_index;
59 };
60
61 struct connman_wispr_portal_context {
62         struct connman_service *service;
63         enum connman_ipconfig_type type;
64         struct connman_wispr_portal *wispr_portal;
65
66         /* Portal/WISPr common */
67         GWeb *web;
68         unsigned int token;
69         guint request_id;
70
71         const char *status_url;
72
73         char *redirect_url;
74
75         /* WISPr specific */
76         GWebParser *wispr_parser;
77         struct connman_wispr_message wispr_msg;
78
79         char *wispr_username;
80         char *wispr_password;
81         char *wispr_formdata;
82
83         enum connman_wispr_result wispr_result;
84
85         GSList *route_list;
86
87         guint timeout;
88 };
89
90 struct connman_wispr_portal {
91         struct connman_wispr_portal_context *ipv4_context;
92         struct connman_wispr_portal_context *ipv6_context;
93 };
94
95 static bool wispr_portal_web_result(GWebResult *result, gpointer user_data);
96
97 static GHashTable *wispr_portal_list = NULL;
98
99 static void connman_wispr_message_init(struct connman_wispr_message *msg)
100 {
101         DBG("");
102
103         msg->has_error = false;
104         msg->current_element = NULL;
105
106         msg->message_type = -1;
107         msg->response_code = -1;
108
109         g_free(msg->login_url);
110         msg->login_url = NULL;
111
112         g_free(msg->abort_login_url);
113         msg->abort_login_url = NULL;
114
115         g_free(msg->logoff_url);
116         msg->logoff_url = NULL;
117
118         g_free(msg->access_procedure);
119         msg->access_procedure = NULL;
120
121         g_free(msg->access_location);
122         msg->access_location = NULL;
123
124         g_free(msg->location_name);
125         msg->location_name = NULL;
126 }
127
128 static void free_wispr_routes(struct connman_wispr_portal_context *wp_context)
129 {
130         while (wp_context->route_list) {
131                 struct wispr_route *route = wp_context->route_list->data;
132
133                 DBG("free route to %s if %d type %d", route->address,
134                                 route->if_index, wp_context->type);
135
136                 switch (wp_context->type) {
137                 case CONNMAN_IPCONFIG_TYPE_IPV4:
138                         connman_inet_del_host_route(route->if_index,
139                                         route->address);
140                         break;
141                 case CONNMAN_IPCONFIG_TYPE_IPV6:
142                         connman_inet_del_ipv6_host_route(route->if_index,
143                                         route->address);
144                         break;
145                 case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
146                 case CONNMAN_IPCONFIG_TYPE_ALL:
147                         break;
148                 }
149
150                 g_free(route->address);
151                 g_free(route);
152
153                 wp_context->route_list =
154                         g_slist_delete_link(wp_context->route_list,
155                                         wp_context->route_list);
156         }
157 }
158
159 static void free_connman_wispr_portal_context(
160                 struct connman_wispr_portal_context *wp_context)
161 {
162         DBG("context %p", wp_context);
163
164         if (!wp_context)
165                 return;
166
167         if (wp_context->wispr_portal) {
168                 if (wp_context->wispr_portal->ipv4_context == wp_context)
169                         wp_context->wispr_portal->ipv4_context = NULL;
170
171                 if (wp_context->wispr_portal->ipv6_context == wp_context)
172                         wp_context->wispr_portal->ipv6_context = NULL;
173         }
174
175         if (wp_context->token > 0)
176                 connman_proxy_lookup_cancel(wp_context->token);
177
178         if (wp_context->request_id > 0)
179                 g_web_cancel_request(wp_context->web, wp_context->request_id);
180
181         if (wp_context->timeout > 0)
182                 g_source_remove(wp_context->timeout);
183
184         if (wp_context->web)
185                 g_web_unref(wp_context->web);
186
187         g_free(wp_context->redirect_url);
188
189         if (wp_context->wispr_parser)
190                 g_web_parser_unref(wp_context->wispr_parser);
191
192         connman_wispr_message_init(&wp_context->wispr_msg);
193
194         g_free(wp_context->wispr_username);
195         g_free(wp_context->wispr_password);
196         g_free(wp_context->wispr_formdata);
197
198         free_wispr_routes(wp_context);
199
200         g_free(wp_context);
201 }
202
203 static struct connman_wispr_portal_context *create_wispr_portal_context(void)
204 {
205         return g_try_new0(struct connman_wispr_portal_context, 1);
206 }
207
208 static void free_connman_wispr_portal(gpointer data)
209 {
210         struct connman_wispr_portal *wispr_portal = data;
211
212         DBG("");
213
214         if (!wispr_portal)
215                 return;
216
217         free_connman_wispr_portal_context(wispr_portal->ipv4_context);
218         free_connman_wispr_portal_context(wispr_portal->ipv6_context);
219
220         g_free(wispr_portal);
221 }
222
223 static const char *message_type_to_string(int message_type)
224 {
225         switch (message_type) {
226         case 100:
227                 return "Initial redirect message";
228         case 110:
229                 return "Proxy notification";
230         case 120:
231                 return "Authentication notification";
232         case 130:
233                 return "Logoff notification";
234         case 140:
235                 return "Response to Authentication Poll";
236         case 150:
237                 return "Response to Abort Login";
238         }
239
240         return NULL;
241 }
242
243 static const char *response_code_to_string(int response_code)
244 {
245         switch (response_code) {
246         case 0:
247                 return "No error";
248         case 50:
249                 return "Login succeeded";
250         case 100:
251                 return "Login failed";
252         case 102:
253                 return "RADIUS server error/timeout";
254         case 105:
255                 return "RADIUS server not enabled";
256         case 150:
257                 return "Logoff succeeded";
258         case 151:
259                 return "Login aborted";
260         case 200:
261                 return "Proxy detection/repeat operation";
262         case 201:
263                 return "Authentication pending";
264         case 255:
265                 return "Access gateway internal error";
266         }
267
268         return NULL;
269 }
270
271 static struct {
272         const char *str;
273         enum {
274                 WISPR_ELEMENT_NONE              = 0,
275                 WISPR_ELEMENT_ACCESS_PROCEDURE  = 1,
276                 WISPR_ELEMENT_ACCESS_LOCATION   = 2,
277                 WISPR_ELEMENT_LOCATION_NAME     = 3,
278                 WISPR_ELEMENT_LOGIN_URL         = 4,
279                 WISPR_ELEMENT_ABORT_LOGIN_URL   = 5,
280                 WISPR_ELEMENT_MESSAGE_TYPE      = 6,
281                 WISPR_ELEMENT_RESPONSE_CODE     = 7,
282                 WISPR_ELEMENT_NEXT_URL          = 8,
283                 WISPR_ELEMENT_DELAY             = 9,
284                 WISPR_ELEMENT_REPLY_MESSAGE     = 10,
285                 WISPR_ELEMENT_LOGIN_RESULTS_URL = 11,
286                 WISPR_ELEMENT_LOGOFF_URL        = 12,
287         } element;
288 } wispr_element_map[] = {
289         { "AccessProcedure",    WISPR_ELEMENT_ACCESS_PROCEDURE  },
290         { "AccessLocation",     WISPR_ELEMENT_ACCESS_LOCATION   },
291         { "LocationName",       WISPR_ELEMENT_LOCATION_NAME     },
292         { "LoginURL",           WISPR_ELEMENT_LOGIN_URL         },
293         { "AbortLoginURL",      WISPR_ELEMENT_ABORT_LOGIN_URL   },
294         { "MessageType",        WISPR_ELEMENT_MESSAGE_TYPE      },
295         { "ResponseCode",       WISPR_ELEMENT_RESPONSE_CODE     },
296         { "NextURL",            WISPR_ELEMENT_NEXT_URL          },
297         { "Delay",              WISPR_ELEMENT_DELAY             },
298         { "ReplyMessage",       WISPR_ELEMENT_REPLY_MESSAGE     },
299         { "LoginResultsURL",    WISPR_ELEMENT_LOGIN_RESULTS_URL },
300         { "LogoffURL",          WISPR_ELEMENT_LOGOFF_URL        },
301         { NULL,                 WISPR_ELEMENT_NONE              },
302 };
303
304 static void xml_wispr_start_element_handler(GMarkupParseContext *context,
305                                         const gchar *element_name,
306                                         const gchar **attribute_names,
307                                         const gchar **attribute_values,
308                                         gpointer user_data, GError **error)
309 {
310         struct connman_wispr_message *msg = user_data;
311
312         msg->current_element = element_name;
313 }
314
315 static void xml_wispr_end_element_handler(GMarkupParseContext *context,
316                                         const gchar *element_name,
317                                         gpointer user_data, GError **error)
318 {
319         struct connman_wispr_message *msg = user_data;
320
321         msg->current_element = NULL;
322 }
323
324 static void xml_wispr_text_handler(GMarkupParseContext *context,
325                                         const gchar *text, gsize text_len,
326                                         gpointer user_data, GError **error)
327 {
328         struct connman_wispr_message *msg = user_data;
329         int i;
330
331         if (!msg->current_element)
332                 return;
333
334         for (i = 0; wispr_element_map[i].str; i++) {
335                 if (!g_str_equal(wispr_element_map[i].str, msg->current_element))
336                         continue;
337
338                 switch (wispr_element_map[i].element) {
339                 case WISPR_ELEMENT_NONE:
340                 case WISPR_ELEMENT_ACCESS_PROCEDURE:
341                         g_free(msg->access_procedure);
342                         msg->access_procedure = g_strdup(text);
343                         break;
344                 case WISPR_ELEMENT_ACCESS_LOCATION:
345                         g_free(msg->access_location);
346                         msg->access_location = g_strdup(text);
347                         break;
348                 case WISPR_ELEMENT_LOCATION_NAME:
349                         g_free(msg->location_name);
350                         msg->location_name = g_strdup(text);
351                         break;
352                 case WISPR_ELEMENT_LOGIN_URL:
353                         g_free(msg->login_url);
354                         msg->login_url = g_strdup(text);
355                         break;
356                 case WISPR_ELEMENT_ABORT_LOGIN_URL:
357                         g_free(msg->abort_login_url);
358                         msg->abort_login_url = g_strdup(text);
359                         break;
360                 case WISPR_ELEMENT_MESSAGE_TYPE:
361                         msg->message_type = atoi(text);
362                         break;
363                 case WISPR_ELEMENT_RESPONSE_CODE:
364                         msg->response_code = atoi(text);
365                         break;
366                 case WISPR_ELEMENT_NEXT_URL:
367                 case WISPR_ELEMENT_DELAY:
368                 case WISPR_ELEMENT_REPLY_MESSAGE:
369                 case WISPR_ELEMENT_LOGIN_RESULTS_URL:
370                         break;
371                 case WISPR_ELEMENT_LOGOFF_URL:
372                         g_free(msg->logoff_url);
373                         msg->logoff_url = g_strdup(text);
374                         break;
375                 }
376         }
377 }
378
379 static void xml_wispr_error_handler(GMarkupParseContext *context,
380                                         GError *error, gpointer user_data)
381 {
382         struct connman_wispr_message *msg = user_data;
383
384         msg->has_error = true;
385 }
386
387 static const GMarkupParser xml_wispr_parser_handlers = {
388         xml_wispr_start_element_handler,
389         xml_wispr_end_element_handler,
390         xml_wispr_text_handler,
391         NULL,
392         xml_wispr_error_handler,
393 };
394
395 static void xml_wispr_parser_callback(const char *str, gpointer user_data)
396 {
397         struct connman_wispr_portal_context *wp_context = user_data;
398         GMarkupParseContext *parser_context = NULL;
399         bool result;
400
401         DBG("");
402
403         parser_context = g_markup_parse_context_new(&xml_wispr_parser_handlers,
404                                         G_MARKUP_TREAT_CDATA_AS_TEXT,
405                                         &(wp_context->wispr_msg), NULL);
406
407         result = g_markup_parse_context_parse(parser_context,
408                                         str, strlen(str), NULL);
409         if (result)
410                 g_markup_parse_context_end_parse(parser_context, NULL);
411
412         g_markup_parse_context_free(parser_context);
413 }
414
415 static void web_debug(const char *str, void *data)
416 {
417         connman_info("%s: %s\n", (const char *) data, str);
418 }
419
420 static void wispr_portal_error(struct connman_wispr_portal_context *wp_context)
421 {
422         DBG("Failed to proceed wispr/portal web request");
423
424         wp_context->wispr_result = CONNMAN_WISPR_RESULT_FAILED;
425 }
426
427 static void portal_manage_status(GWebResult *result,
428                         struct connman_wispr_portal_context *wp_context)
429 {
430         struct connman_service *service = wp_context->service;
431         enum connman_ipconfig_type type = wp_context->type;
432         const char *str = NULL;
433
434         DBG("");
435
436         /* We currently don't do anything with this info */
437         if (g_web_result_get_header(result, "X-ConnMan-Client-IP",
438                                 &str))
439                 connman_info("Client-IP: %s", str);
440
441         if (g_web_result_get_header(result, "X-ConnMan-Client-Country",
442                                 &str))
443                 connman_info("Client-Country: %s", str);
444
445         if (g_web_result_get_header(result, "X-ConnMan-Client-Region",
446                                 &str))
447                 connman_info("Client-Region: %s", str);
448
449         if (g_web_result_get_header(result, "X-ConnMan-Client-Timezone",
450                                 &str))
451                 connman_info("Client-Timezone: %s", str);
452
453         free_connman_wispr_portal_context(wp_context);
454
455         __connman_service_ipconfig_indicate_state(service,
456                                         CONNMAN_SERVICE_STATE_ONLINE, type);
457 }
458
459 static bool wispr_route_request(const char *address, int ai_family,
460                 int if_index, gpointer user_data)
461 {
462         int result = -1;
463         struct connman_wispr_portal_context *wp_context = user_data;
464         const char *gateway;
465         struct wispr_route *route;
466
467         gateway = __connman_ipconfig_get_gateway_from_index(if_index,
468                 wp_context->type);
469
470         DBG("address %s if %d gw %s", address, if_index, gateway);
471
472         if (!gateway)
473                 return false;
474
475         route = g_try_new0(struct wispr_route, 1);
476         if (route == 0) {
477                 DBG("could not create struct");
478                 return false;
479         }
480
481         switch (wp_context->type) {
482         case CONNMAN_IPCONFIG_TYPE_IPV4:
483                 result = connman_inet_add_host_route(if_index, address,
484                                 gateway);
485                 break;
486         case CONNMAN_IPCONFIG_TYPE_IPV6:
487                 result = connman_inet_add_ipv6_host_route(if_index, address,
488                                 gateway);
489                 break;
490         case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
491         case CONNMAN_IPCONFIG_TYPE_ALL:
492                 break;
493         }
494
495         if (result < 0) {
496                 g_free(route);
497                 return false;
498         }
499
500         route->address = g_strdup(address);
501         route->if_index = if_index;
502         wp_context->route_list = g_slist_prepend(wp_context->route_list, route);
503
504         return true;
505 }
506
507 static void wispr_portal_request_portal(
508                 struct connman_wispr_portal_context *wp_context)
509 {
510         DBG("");
511
512         wp_context->request_id = g_web_request_get(wp_context->web,
513                                         wp_context->status_url,
514                                         wispr_portal_web_result,
515                                         wispr_route_request,
516                                         wp_context);
517
518         if (wp_context->request_id == 0)
519                 wispr_portal_error(wp_context);
520 }
521
522 static bool wispr_input(const guint8 **data, gsize *length,
523                                                 gpointer user_data)
524 {
525         struct connman_wispr_portal_context *wp_context = user_data;
526         GString *buf;
527         gsize count;
528
529         DBG("");
530
531         buf = g_string_sized_new(100);
532
533         g_string_append(buf, "button=Login&UserName=");
534         g_string_append_uri_escaped(buf, wp_context->wispr_username,
535                                                                 NULL, FALSE);
536         g_string_append(buf, "&Password=");
537         g_string_append_uri_escaped(buf, wp_context->wispr_password,
538                                                                 NULL, FALSE);
539         g_string_append(buf, "&FNAME=0&OriginatingServer=");
540         g_string_append_uri_escaped(buf, wp_context->status_url, NULL, FALSE);
541
542         count = buf->len;
543
544         g_free(wp_context->wispr_formdata);
545         wp_context->wispr_formdata = g_string_free(buf, FALSE);
546
547         *data = (guint8 *) wp_context->wispr_formdata;
548         *length = count;
549
550         return false;
551 }
552
553 static void wispr_portal_browser_reply_cb(struct connman_service *service,
554                                         bool authentication_done,
555                                         const char *error, void *user_data)
556 {
557         struct connman_wispr_portal_context *wp_context = user_data;
558         struct connman_wispr_portal *wispr_portal;
559         int index;
560
561         DBG("");
562
563         if (!service || !wp_context)
564                 return;
565
566         /*
567          * No way to cancel this if wp_context has been freed, so we lookup
568          * from the service and check that this is still the right context.
569          */
570         index = __connman_service_get_index(service);
571         if (index < 0)
572                 return;
573
574         wispr_portal = g_hash_table_lookup(wispr_portal_list,
575                                         GINT_TO_POINTER(index));
576         if (!wispr_portal)
577                 return;
578
579         if (wp_context != wispr_portal->ipv4_context &&
580                 wp_context != wispr_portal->ipv6_context)
581                 return;
582
583         if (!authentication_done) {
584                 wispr_portal_error(wp_context);
585                 free_wispr_routes(wp_context);
586                 return;
587         }
588
589         /* Restarting the test */
590         __connman_service_wispr_start(service, wp_context->type);
591 }
592
593 static void wispr_portal_request_wispr_login(struct connman_service *service,
594                                 bool success,
595                                 const char *ssid, int ssid_len,
596                                 const char *username, const char *password,
597                                 bool wps, const char *wpspin,
598                                 const char *error, void *user_data)
599 {
600         struct connman_wispr_portal_context *wp_context = user_data;
601
602         DBG("");
603
604         if (error) {
605                 if (g_strcmp0(error,
606                         "net.connman.Agent.Error.LaunchBrowser") == 0) {
607                         if (__connman_agent_request_browser(service,
608                                         wispr_portal_browser_reply_cb,
609                                         wp_context->redirect_url,
610                                         wp_context) == -EINPROGRESS)
611                                 return;
612                 }
613
614                 free_connman_wispr_portal_context(wp_context);
615                 return;
616         }
617
618         g_free(wp_context->wispr_username);
619         wp_context->wispr_username = g_strdup(username);
620
621         g_free(wp_context->wispr_password);
622         wp_context->wispr_password = g_strdup(password);
623
624         wp_context->request_id = g_web_request_post(wp_context->web,
625                                         wp_context->wispr_msg.login_url,
626                                         "application/x-www-form-urlencoded",
627                                         wispr_input, wispr_portal_web_result,
628                                         wp_context);
629
630         connman_wispr_message_init(&wp_context->wispr_msg);
631 }
632
633 static bool wispr_manage_message(GWebResult *result,
634                         struct connman_wispr_portal_context *wp_context)
635 {
636         DBG("Message type: %s (%d)",
637                 message_type_to_string(wp_context->wispr_msg.message_type),
638                                         wp_context->wispr_msg.message_type);
639         DBG("Response code: %s (%d)",
640                 response_code_to_string(wp_context->wispr_msg.response_code),
641                                         wp_context->wispr_msg.response_code);
642
643         if (wp_context->wispr_msg.access_procedure)
644                 DBG("Access procedure: %s",
645                         wp_context->wispr_msg.access_procedure);
646         if (wp_context->wispr_msg.access_location)
647                 DBG("Access location: %s",
648                         wp_context->wispr_msg.access_location);
649         if (wp_context->wispr_msg.location_name)
650                 DBG("Location name: %s",
651                         wp_context->wispr_msg.location_name);
652         if (wp_context->wispr_msg.login_url)
653                 DBG("Login URL: %s", wp_context->wispr_msg.login_url);
654         if (wp_context->wispr_msg.abort_login_url)
655                 DBG("Abort login URL: %s",
656                         wp_context->wispr_msg.abort_login_url);
657         if (wp_context->wispr_msg.logoff_url)
658                 DBG("Logoff URL: %s", wp_context->wispr_msg.logoff_url);
659
660         switch (wp_context->wispr_msg.message_type) {
661         case 100:
662                 DBG("Login required");
663
664                 wp_context->wispr_result = CONNMAN_WISPR_RESULT_LOGIN;
665
666                 if (__connman_agent_request_login_input(wp_context->service,
667                                         wispr_portal_request_wispr_login,
668                                         wp_context) != -EINPROGRESS)
669                         wispr_portal_error(wp_context);
670                 else
671                         return true;
672
673                 break;
674         case 120: /* Falling down */
675         case 140:
676                 if (wp_context->wispr_msg.response_code == 50) {
677                         wp_context->wispr_result = CONNMAN_WISPR_RESULT_ONLINE;
678
679                         g_free(wp_context->wispr_username);
680                         wp_context->wispr_username = NULL;
681
682                         g_free(wp_context->wispr_password);
683                         wp_context->wispr_password = NULL;
684
685                         g_free(wp_context->wispr_formdata);
686                         wp_context->wispr_formdata = NULL;
687
688                         wispr_portal_request_portal(wp_context);
689
690                         return true;
691                 } else
692                         wispr_portal_error(wp_context);
693
694                 break;
695         default:
696                 break;
697         }
698
699         return false;
700 }
701
702 static bool wispr_portal_web_result(GWebResult *result, gpointer user_data)
703 {
704         struct connman_wispr_portal_context *wp_context = user_data;
705         const char *redirect = NULL;
706         const guint8 *chunk = NULL;
707         const char *str = NULL;
708         guint16 status;
709         gsize length;
710
711         DBG("");
712
713         if (wp_context->wispr_result != CONNMAN_WISPR_RESULT_ONLINE) {
714                 g_web_result_get_chunk(result, &chunk, &length);
715
716                 if (length > 0) {
717                         g_web_parser_feed_data(wp_context->wispr_parser,
718                                                                 chunk, length);
719                         return true;
720                 }
721
722                 g_web_parser_end_data(wp_context->wispr_parser);
723
724                 if (wp_context->wispr_msg.message_type >= 0) {
725                         if (wispr_manage_message(result, wp_context))
726                                 goto done;
727                 }
728         }
729
730         status = g_web_result_get_status(result);
731
732         DBG("status: %03u", status);
733
734         switch (status) {
735         case 000:
736                 __connman_agent_request_browser(wp_context->service,
737                                 wispr_portal_browser_reply_cb,
738                                 wp_context->status_url, wp_context);
739                 break;
740         case 200:
741                 if (wp_context->wispr_msg.message_type >= 0)
742                         break;
743
744                 if (g_web_result_get_header(result, "X-ConnMan-Status",
745                                                 &str)) {
746                         portal_manage_status(result, wp_context);
747                         return false;
748                 } else
749                         __connman_agent_request_browser(wp_context->service,
750                                         wispr_portal_browser_reply_cb,
751                                         wp_context->redirect_url, wp_context);
752
753                 break;
754         case 302:
755                 if (!g_web_supports_tls() ||
756                         !g_web_result_get_header(result, "Location",
757                                                         &redirect)) {
758
759                         __connman_agent_request_browser(wp_context->service,
760                                         wispr_portal_browser_reply_cb,
761                                         wp_context->status_url, wp_context);
762                         break;
763                 }
764
765                 DBG("Redirect URL: %s", redirect);
766
767                 wp_context->redirect_url = g_strdup(redirect);
768
769                 wp_context->request_id = g_web_request_get(wp_context->web,
770                                 redirect, wispr_portal_web_result,
771                                 wispr_route_request, wp_context);
772
773                 goto done;
774         case 400:
775         case 404:
776                 if (__connman_service_online_check_failed(wp_context->service,
777                                                 wp_context->type) == 0) {
778                         wispr_portal_error(wp_context);
779                         free_connman_wispr_portal_context(wp_context);
780                         return false;
781                 }
782
783                 break;
784         case 505:
785                 __connman_agent_request_browser(wp_context->service,
786                                 wispr_portal_browser_reply_cb,
787                                 wp_context->status_url, wp_context);
788                 break;
789         default:
790                 break;
791         }
792
793         free_wispr_routes(wp_context);
794         wp_context->request_id = 0;
795 done:
796         wp_context->wispr_msg.message_type = -1;
797         return false;
798 }
799
800 static void proxy_callback(const char *proxy, void *user_data)
801 {
802         struct connman_wispr_portal_context *wp_context = user_data;
803
804         DBG("proxy %s", proxy);
805
806         if (!wp_context)
807                 return;
808
809         wp_context->token = 0;
810
811         if (proxy && g_strcmp0(proxy, "DIRECT") != 0) {
812                 if (g_str_has_prefix(proxy, "PROXY")) {
813                         proxy += 5;
814                         for (; *proxy == ' ' && *proxy != '\0'; proxy++);
815                 }
816                 g_web_set_proxy(wp_context->web, proxy);
817         }
818
819         g_web_set_accept(wp_context->web, NULL);
820         g_web_set_user_agent(wp_context->web, "ConnMan/%s wispr", VERSION);
821         g_web_set_close_connection(wp_context->web, TRUE);
822
823         connman_wispr_message_init(&wp_context->wispr_msg);
824
825         wp_context->wispr_parser = g_web_parser_new(
826                                         "<WISPAccessGatewayParam",
827                                         "WISPAccessGatewayParam>",
828                                         xml_wispr_parser_callback, wp_context);
829
830         wispr_portal_request_portal(wp_context);
831 }
832
833 static gboolean no_proxy_callback(gpointer user_data)
834 {
835         struct connman_wispr_portal_context *wp_context = user_data;
836
837         wp_context->timeout = 0;
838
839         proxy_callback("DIRECT", wp_context);
840
841         return FALSE;
842 }
843
844 static int wispr_portal_detect(struct connman_wispr_portal_context *wp_context)
845 {
846         enum connman_service_proxy_method proxy_method;
847         enum connman_service_type service_type;
848         char *interface = NULL;
849         char **nameservers = NULL;
850         int if_index;
851         int err = 0;
852         int i;
853
854         DBG("wispr/portal context %p service %p", wp_context,
855                 wp_context->service);
856
857         service_type = connman_service_get_type(wp_context->service);
858
859         switch (service_type) {
860         case CONNMAN_SERVICE_TYPE_ETHERNET:
861         case CONNMAN_SERVICE_TYPE_WIFI:
862         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
863         case CONNMAN_SERVICE_TYPE_CELLULAR:
864         case CONNMAN_SERVICE_TYPE_GADGET:
865                 break;
866         case CONNMAN_SERVICE_TYPE_UNKNOWN:
867         case CONNMAN_SERVICE_TYPE_SYSTEM:
868         case CONNMAN_SERVICE_TYPE_GPS:
869         case CONNMAN_SERVICE_TYPE_VPN:
870         case CONNMAN_SERVICE_TYPE_P2P:
871                 return -EOPNOTSUPP;
872         }
873
874         interface = connman_service_get_interface(wp_context->service);
875         if (!interface)
876                 return -EINVAL;
877
878         DBG("interface %s", interface);
879
880         if_index = connman_inet_ifindex(interface);
881         if (if_index < 0) {
882                 DBG("Could not get ifindex");
883                 err = -EINVAL;
884                 goto done;
885         }
886
887         nameservers = connman_service_get_nameservers(wp_context->service);
888         if (!nameservers) {
889                 DBG("Could not get nameservers");
890                 err = -EINVAL;
891                 goto done;
892         }
893
894         wp_context->web = g_web_new(if_index);
895         if (!wp_context->web) {
896                 DBG("Could not set up GWeb");
897                 err = -ENOMEM;
898                 goto done;
899         }
900
901         if (getenv("CONNMAN_WEB_DEBUG"))
902                 g_web_set_debug(wp_context->web, web_debug, "WEB");
903
904         if (wp_context->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
905                 g_web_set_address_family(wp_context->web, AF_INET);
906                 wp_context->status_url = STATUS_URL_IPV4;
907         } else {
908                 g_web_set_address_family(wp_context->web, AF_INET6);
909                 wp_context->status_url = STATUS_URL_IPV6;
910         }
911
912         for (i = 0; nameservers[i]; i++)
913                 g_web_add_nameserver(wp_context->web, nameservers[i]);
914
915         proxy_method = connman_service_get_proxy_method(wp_context->service);
916
917         if (proxy_method != CONNMAN_SERVICE_PROXY_METHOD_DIRECT) {
918                 wp_context->token = connman_proxy_lookup(interface,
919                                                 wp_context->status_url,
920                                                 wp_context->service,
921                                                 proxy_callback, wp_context);
922
923                 if (wp_context->token == 0) {
924                         err = -EINVAL;
925                         free_connman_wispr_portal_context(wp_context);
926                 }
927         } else if (wp_context->timeout == 0) {
928                 wp_context->timeout = g_idle_add(no_proxy_callback, wp_context);
929         }
930
931 done:
932         g_strfreev(nameservers);
933
934         g_free(interface);
935         return err;
936 }
937
938 int __connman_wispr_start(struct connman_service *service,
939                                         enum connman_ipconfig_type type)
940 {
941         struct connman_wispr_portal_context *wp_context = NULL;
942         struct connman_wispr_portal *wispr_portal = NULL;
943         int index;
944
945         DBG("service %p", service);
946
947         if (!wispr_portal_list)
948                 return -EINVAL;
949
950         index = __connman_service_get_index(service);
951         if (index < 0)
952                 return -EINVAL;
953
954         wispr_portal = g_hash_table_lookup(wispr_portal_list,
955                                         GINT_TO_POINTER(index));
956         if (!wispr_portal) {
957                 wispr_portal = g_try_new0(struct connman_wispr_portal, 1);
958                 if (!wispr_portal)
959                         return -ENOMEM;
960
961                 g_hash_table_replace(wispr_portal_list,
962                                         GINT_TO_POINTER(index), wispr_portal);
963         }
964
965         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
966                 wp_context = wispr_portal->ipv4_context;
967         else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
968                 wp_context = wispr_portal->ipv6_context;
969         else
970                 return -EINVAL;
971
972         /* If there is already an existing context, we wipe it */
973         if (wp_context)
974                 free_connman_wispr_portal_context(wp_context);
975
976         wp_context = create_wispr_portal_context();
977         if (!wp_context)
978                 return -ENOMEM;
979
980         wp_context->service = service;
981         wp_context->type = type;
982         wp_context->wispr_portal = wispr_portal;
983
984         if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
985                 wispr_portal->ipv4_context = wp_context;
986         else
987                 wispr_portal->ipv6_context = wp_context;
988
989         return wispr_portal_detect(wp_context);
990 }
991
992 void __connman_wispr_stop(struct connman_service *service)
993 {
994         int index;
995
996         DBG("service %p", service);
997
998         if (!wispr_portal_list)
999                 return;
1000
1001         index = __connman_service_get_index(service);
1002         if (index < 0)
1003                 return;
1004
1005         g_hash_table_remove(wispr_portal_list, GINT_TO_POINTER(index));
1006 }
1007
1008 int __connman_wispr_init(void)
1009 {
1010         DBG("");
1011
1012         wispr_portal_list = g_hash_table_new_full(g_direct_hash,
1013                                                 g_direct_equal, NULL,
1014                                                 free_connman_wispr_portal);
1015
1016         return 0;
1017 }
1018
1019 void __connman_wispr_cleanup(void)
1020 {
1021         DBG("");
1022
1023         g_hash_table_destroy(wispr_portal_list);
1024         wispr_portal_list = NULL;
1025 }