wispr.c 25.4 KB
Newer Older
1 2 3 4
/*
 *
 *  Connection Manager
 *
5
 *  Copyright (C) 2007-2013  Intel Corporation. All rights reserved.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <errno.h>
#include <stdlib.h>

#include <gweb/gweb.h>

#include "connman.h"
32
#include "agent.h"
33 34

struct connman_wispr_message {
35
	bool has_error;
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
	const char *current_element;
	int message_type;
	int response_code;
	char *login_url;
	char *abort_login_url;
	char *logoff_url;
	char *access_procedure;
	char *access_location;
	char *location_name;
};

enum connman_wispr_result {
	CONNMAN_WISPR_RESULT_UNKNOWN = 0,
	CONNMAN_WISPR_RESULT_LOGIN   = 1,
	CONNMAN_WISPR_RESULT_ONLINE  = 2,
	CONNMAN_WISPR_RESULT_FAILED  = 3,
};

struct wispr_route {
	char *address;
	int if_index;
};

struct connman_wispr_portal_context {
	struct connman_service *service;
	enum connman_ipconfig_type type;
	struct connman_wispr_portal *wispr_portal;

	/* Portal/WISPr common */
	GWeb *web;
	unsigned int token;
	guint request_id;

	const char *status_url;

	char *redirect_url;

	/* WISPr specific */
	GWebParser *wispr_parser;
	struct connman_wispr_message wispr_msg;

	char *wispr_username;
	char *wispr_password;
	char *wispr_formdata;

	enum connman_wispr_result wispr_result;

	GSList *route_list;

	guint timeout;
};

struct connman_wispr_portal {
	struct connman_wispr_portal_context *ipv4_context;
	struct connman_wispr_portal_context *ipv6_context;
};

93
static bool wispr_portal_web_result(GWebResult *result, gpointer user_data);
94 95 96 97 98 99 100

static GHashTable *wispr_portal_list = NULL;

static void connman_wispr_message_init(struct connman_wispr_message *msg)
{
	DBG("");

101
	msg->has_error = false;
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
	msg->current_element = NULL;

	msg->message_type = -1;
	msg->response_code = -1;

	g_free(msg->login_url);
	msg->login_url = NULL;

	g_free(msg->abort_login_url);
	msg->abort_login_url = NULL;

	g_free(msg->logoff_url);
	msg->logoff_url = NULL;

	g_free(msg->access_procedure);
	msg->access_procedure = NULL;

	g_free(msg->access_location);
	msg->access_location = NULL;

	g_free(msg->location_name);
	msg->location_name = NULL;
}

static void free_wispr_routes(struct connman_wispr_portal_context *wp_context)
{
128
	DBG("wp_context %p",wp_context);
129
	while (wp_context->route_list) {
130 131
		if (!wp_context->route_list->data)
		continue;
132 133 134 135 136
		struct wispr_route *route = wp_context->route_list->data;

		DBG("free route to %s if %d type %d", route->address,
				route->if_index, wp_context->type);

137
		switch (wp_context->type) {
138 139 140 141 142 143 144 145 146
		case CONNMAN_IPCONFIG_TYPE_IPV4:
			connman_inet_del_host_route(route->if_index,
					route->address);
			break;
		case CONNMAN_IPCONFIG_TYPE_IPV6:
			connman_inet_del_ipv6_host_route(route->if_index,
					route->address);
			break;
		case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
147
		case CONNMAN_IPCONFIG_TYPE_ALL:
148 149 150 151 152 153 154 155 156 157 158 159
			break;
		}

		g_free(route->address);
		g_free(route);

		wp_context->route_list =
			g_slist_delete_link(wp_context->route_list,
					wp_context->route_list);
	}
}

160 161
static void free_connman_wispr_portal_context(
		struct connman_wispr_portal_context *wp_context)
162 163 164
{
	DBG("context %p", wp_context);

165
	if (!wp_context)
166 167
		return;

168
	if (wp_context->wispr_portal) {
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
		if (wp_context->wispr_portal->ipv4_context == wp_context)
			wp_context->wispr_portal->ipv4_context = NULL;

		if (wp_context->wispr_portal->ipv6_context == wp_context)
			wp_context->wispr_portal->ipv6_context = NULL;
	}

	if (wp_context->token > 0)
		connman_proxy_lookup_cancel(wp_context->token);

	if (wp_context->request_id > 0)
		g_web_cancel_request(wp_context->web, wp_context->request_id);

	if (wp_context->timeout > 0)
		g_source_remove(wp_context->timeout);

185
	if (wp_context->web)
186 187 188 189
		g_web_unref(wp_context->web);

	g_free(wp_context->redirect_url);

190
	if (wp_context->wispr_parser)
191 192 193 194 195 196 197 198 199 200
		g_web_parser_unref(wp_context->wispr_parser);

	connman_wispr_message_init(&wp_context->wispr_msg);

	g_free(wp_context->wispr_username);
	g_free(wp_context->wispr_password);
	g_free(wp_context->wispr_formdata);

	free_wispr_routes(wp_context);

201 202
	connman_service_unref(wp_context->service);

203 204 205 206 207 208 209 210 211 212 213 214 215 216
	g_free(wp_context);
}

static struct connman_wispr_portal_context *create_wispr_portal_context(void)
{
	return g_try_new0(struct connman_wispr_portal_context, 1);
}

static void free_connman_wispr_portal(gpointer data)
{
	struct connman_wispr_portal *wispr_portal = data;

	DBG("");

217
	if (!wispr_portal)
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
		return;

	free_connman_wispr_portal_context(wispr_portal->ipv4_context);
	free_connman_wispr_portal_context(wispr_portal->ipv6_context);

	g_free(wispr_portal);
}

static const char *message_type_to_string(int message_type)
{
	switch (message_type) {
	case 100:
		return "Initial redirect message";
	case 110:
		return "Proxy notification";
	case 120:
		return "Authentication notification";
	case 130:
		return "Logoff notification";
	case 140:
		return "Response to Authentication Poll";
	case 150:
		return "Response to Abort Login";
	}

	return NULL;
}

static const char *response_code_to_string(int response_code)
{
	switch (response_code) {
	case 0:
		return "No error";
	case 50:
		return "Login succeeded";
	case 100:
		return "Login failed";
	case 102:
		return "RADIUS server error/timeout";
	case 105:
		return "RADIUS server not enabled";
	case 150:
		return "Logoff succeeded";
	case 151:
		return "Login aborted";
	case 200:
		return "Proxy detection/repeat operation";
	case 201:
		return "Authentication pending";
Pasi Sjöholm's avatar
Pasi Sjöholm committed
267 268
	case 204:
		return "Walled garden check";
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
	case 255:
		return "Access gateway internal error";
	}

	return NULL;
}

static struct {
	const char *str;
	enum {
		WISPR_ELEMENT_NONE              = 0,
		WISPR_ELEMENT_ACCESS_PROCEDURE  = 1,
		WISPR_ELEMENT_ACCESS_LOCATION   = 2,
		WISPR_ELEMENT_LOCATION_NAME     = 3,
		WISPR_ELEMENT_LOGIN_URL         = 4,
		WISPR_ELEMENT_ABORT_LOGIN_URL   = 5,
		WISPR_ELEMENT_MESSAGE_TYPE      = 6,
		WISPR_ELEMENT_RESPONSE_CODE     = 7,
		WISPR_ELEMENT_NEXT_URL          = 8,
		WISPR_ELEMENT_DELAY             = 9,
		WISPR_ELEMENT_REPLY_MESSAGE     = 10,
		WISPR_ELEMENT_LOGIN_RESULTS_URL = 11,
		WISPR_ELEMENT_LOGOFF_URL        = 12,
	} element;
} wispr_element_map[] = {
	{ "AccessProcedure",	WISPR_ELEMENT_ACCESS_PROCEDURE	},
	{ "AccessLocation",	WISPR_ELEMENT_ACCESS_LOCATION	},
	{ "LocationName",	WISPR_ELEMENT_LOCATION_NAME	},
	{ "LoginURL",		WISPR_ELEMENT_LOGIN_URL		},
	{ "AbortLoginURL",	WISPR_ELEMENT_ABORT_LOGIN_URL	},
	{ "MessageType",	WISPR_ELEMENT_MESSAGE_TYPE	},
	{ "ResponseCode",	WISPR_ELEMENT_RESPONSE_CODE	},
	{ "NextURL",		WISPR_ELEMENT_NEXT_URL		},
	{ "Delay",		WISPR_ELEMENT_DELAY		},
	{ "ReplyMessage",	WISPR_ELEMENT_REPLY_MESSAGE	},
	{ "LoginResultsURL",	WISPR_ELEMENT_LOGIN_RESULTS_URL	},
	{ "LogoffURL",		WISPR_ELEMENT_LOGOFF_URL	},
	{ NULL,			WISPR_ELEMENT_NONE		},
};

static void xml_wispr_start_element_handler(GMarkupParseContext *context,
					const gchar *element_name,
					const gchar **attribute_names,
					const gchar **attribute_values,
					gpointer user_data, GError **error)
{
	struct connman_wispr_message *msg = user_data;

	msg->current_element = element_name;
}

static void xml_wispr_end_element_handler(GMarkupParseContext *context,
					const gchar *element_name,
					gpointer user_data, GError **error)
{
	struct connman_wispr_message *msg = user_data;

	msg->current_element = NULL;
}

static void xml_wispr_text_handler(GMarkupParseContext *context,
					const gchar *text, gsize text_len,
					gpointer user_data, GError **error)
{
	struct connman_wispr_message *msg = user_data;
	int i;

336
	if (!msg->current_element)
337 338 339
		return;

	for (i = 0; wispr_element_map[i].str; i++) {
340
		if (!g_str_equal(wispr_element_map[i].str, msg->current_element))
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
			continue;

		switch (wispr_element_map[i].element) {
		case WISPR_ELEMENT_NONE:
		case WISPR_ELEMENT_ACCESS_PROCEDURE:
			g_free(msg->access_procedure);
			msg->access_procedure = g_strdup(text);
			break;
		case WISPR_ELEMENT_ACCESS_LOCATION:
			g_free(msg->access_location);
			msg->access_location = g_strdup(text);
			break;
		case WISPR_ELEMENT_LOCATION_NAME:
			g_free(msg->location_name);
			msg->location_name = g_strdup(text);
			break;
		case WISPR_ELEMENT_LOGIN_URL:
			g_free(msg->login_url);
			msg->login_url = g_strdup(text);
			break;
		case WISPR_ELEMENT_ABORT_LOGIN_URL:
			g_free(msg->abort_login_url);
			msg->abort_login_url = g_strdup(text);
			break;
		case WISPR_ELEMENT_MESSAGE_TYPE:
			msg->message_type = atoi(text);
			break;
		case WISPR_ELEMENT_RESPONSE_CODE:
			msg->response_code = atoi(text);
			break;
		case WISPR_ELEMENT_NEXT_URL:
		case WISPR_ELEMENT_DELAY:
		case WISPR_ELEMENT_REPLY_MESSAGE:
		case WISPR_ELEMENT_LOGIN_RESULTS_URL:
			break;
		case WISPR_ELEMENT_LOGOFF_URL:
			g_free(msg->logoff_url);
			msg->logoff_url = g_strdup(text);
			break;
		}
	}
}

static void xml_wispr_error_handler(GMarkupParseContext *context,
					GError *error, gpointer user_data)
{
	struct connman_wispr_message *msg = user_data;

389
	msg->has_error = true;
390 391 392 393 394 395 396 397 398 399 400 401 402 403
}

static const GMarkupParser xml_wispr_parser_handlers = {
	xml_wispr_start_element_handler,
	xml_wispr_end_element_handler,
	xml_wispr_text_handler,
	NULL,
	xml_wispr_error_handler,
};

static void xml_wispr_parser_callback(const char *str, gpointer user_data)
{
	struct connman_wispr_portal_context *wp_context = user_data;
	GMarkupParseContext *parser_context = NULL;
404
	bool result;
405 406 407 408 409 410 411 412 413

	DBG("");

	parser_context = g_markup_parse_context_new(&xml_wispr_parser_handlers,
					G_MARKUP_TREAT_CDATA_AS_TEXT,
					&(wp_context->wispr_msg), NULL);

	result = g_markup_parse_context_parse(parser_context,
					str, strlen(str), NULL);
414
	if (result)
415 416 417 418 419 420 421
		g_markup_parse_context_end_parse(parser_context, NULL);

	g_markup_parse_context_free(parser_context);
}

static void web_debug(const char *str, void *data)
{
422
	DBG("%s: %s\n", (const char *) data, str);
423 424 425 426 427 428 429 430 431 432 433 434 435
}

static void wispr_portal_error(struct connman_wispr_portal_context *wp_context)
{
	DBG("Failed to proceed wispr/portal web request");

	wp_context->wispr_result = CONNMAN_WISPR_RESULT_FAILED;
}

static void portal_manage_status(GWebResult *result,
			struct connman_wispr_portal_context *wp_context)
{
	enum connman_ipconfig_type type = wp_context->type;
436
	struct connman_service *service;
437 438 439 440 441 442
	const char *str = NULL;

	DBG("");

	/* We currently don't do anything with this info */
	if (g_web_result_get_header(result, "X-ConnMan-Client-IP",
443
				&str))
444
		DBG("Client-IP: %s", str);
445 446

	if (g_web_result_get_header(result, "X-ConnMan-Client-Country",
447
				&str))
448
		DBG("Client-Country: %s", str);
449 450

	if (g_web_result_get_header(result, "X-ConnMan-Client-Region",
451
				&str))
452
		DBG("Client-Region: %s", str);
453 454

	if (g_web_result_get_header(result, "X-ConnMan-Client-Timezone",
455
				&str))
456
		DBG("Client-Timezone: %s", str);
457

458 459 460 461
	/* __connman_service_ipconfig_indicate_state may end up calling
	 * __connman_wispr_start which would reinitialize the wispr context
	 * so we better free it beforehand to avoid deallocating it twice. */
	service = connman_service_ref(wp_context->service);
462
	free_connman_wispr_portal_context(wp_context);
463 464 465
	__connman_service_ipconfig_indicate_state(service,
					CONNMAN_SERVICE_STATE_ONLINE, type);
	connman_service_unref(service);
466 467
}

468
static bool wispr_route_request(const char *address, int ai_family,
469 470 471 472 473 474 475 476 477 478 479 480
		int if_index, gpointer user_data)
{
	int result = -1;
	struct connman_wispr_portal_context *wp_context = user_data;
	const char *gateway;
	struct wispr_route *route;

	gateway = __connman_ipconfig_get_gateway_from_index(if_index,
		wp_context->type);

	DBG("address %s if %d gw %s", address, if_index, gateway);

481 482
	if (!gateway)
		return false;
483 484 485 486

	route = g_try_new0(struct wispr_route, 1);
	if (route == 0) {
		DBG("could not create struct");
487
		return false;
488 489
	}

490
	switch (wp_context->type) {
491 492 493 494 495 496 497 498 499
	case CONNMAN_IPCONFIG_TYPE_IPV4:
		result = connman_inet_add_host_route(if_index, address,
				gateway);
		break;
	case CONNMAN_IPCONFIG_TYPE_IPV6:
		result = connman_inet_add_ipv6_host_route(if_index, address,
				gateway);
		break;
	case CONNMAN_IPCONFIG_TYPE_UNKNOWN:
500
	case CONNMAN_IPCONFIG_TYPE_ALL:
501 502 503 504 505
		break;
	}

	if (result < 0) {
		g_free(route);
506
		return false;
507 508 509 510 511 512
	}

	route->address = g_strdup(address);
	route->if_index = if_index;
	wp_context->route_list = g_slist_prepend(wp_context->route_list, route);

513
	return true;
514 515
}

516 517
static void wispr_portal_request_portal(
		struct connman_wispr_portal_context *wp_context)
518 519 520 521 522 523 524 525 526 527 528 529 530
{
	DBG("");

	wp_context->request_id = g_web_request_get(wp_context->web,
					wp_context->status_url,
					wispr_portal_web_result,
					wispr_route_request,
					wp_context);

	if (wp_context->request_id == 0)
		wispr_portal_error(wp_context);
}

531
static bool wispr_input(const guint8 **data, gsize *length,
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
						gpointer user_data)
{
	struct connman_wispr_portal_context *wp_context = user_data;
	GString *buf;
	gsize count;

	DBG("");

	buf = g_string_sized_new(100);

	g_string_append(buf, "button=Login&UserName=");
	g_string_append_uri_escaped(buf, wp_context->wispr_username,
								NULL, FALSE);
	g_string_append(buf, "&Password=");
	g_string_append_uri_escaped(buf, wp_context->wispr_password,
								NULL, FALSE);
	g_string_append(buf, "&FNAME=0&OriginatingServer=");
	g_string_append_uri_escaped(buf, wp_context->status_url, NULL, FALSE);

	count = buf->len;

	g_free(wp_context->wispr_formdata);
	wp_context->wispr_formdata = g_string_free(buf, FALSE);

	*data = (guint8 *) wp_context->wispr_formdata;
	*length = count;

559
	return false;
560 561 562
}

static void wispr_portal_browser_reply_cb(struct connman_service *service,
563
					bool authentication_done,
564 565 566 567
					const char *error, void *user_data)
{
	struct connman_wispr_portal_context *wp_context = user_data;

568
	DBG("authentication_done %d", authentication_done);
569

570
	if (!service || !wp_context || !authentication_done)
571 572 573 574 575 576 577
		return;

	/* Restarting the test */
	__connman_wispr_start(service, wp_context->type);
}

static void wispr_portal_request_wispr_login(struct connman_service *service,
578
				bool success,
579 580
				const char *ssid, int ssid_len,
				const char *username, const char *password,
581
				bool wps, const char *wpspin,
582 583 584 585 586 587
				const char *error, void *user_data)
{
	struct connman_wispr_portal_context *wp_context = user_data;

	DBG("");

588
	if (error) {
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
		if (g_strcmp0(error,
			"net.connman.Agent.Error.LaunchBrowser") == 0) {
			if (__connman_agent_request_browser(service,
					wispr_portal_browser_reply_cb,
					wp_context->redirect_url,
					wp_context) == -EINPROGRESS)
				return;
		}

		free_connman_wispr_portal_context(wp_context);
		return;
	}

	g_free(wp_context->wispr_username);
	wp_context->wispr_username = g_strdup(username);

	g_free(wp_context->wispr_password);
	wp_context->wispr_password = g_strdup(password);

	wp_context->request_id = g_web_request_post(wp_context->web,
					wp_context->wispr_msg.login_url,
					"application/x-www-form-urlencoded",
					wispr_input, wispr_portal_web_result,
					wp_context);

	connman_wispr_message_init(&wp_context->wispr_msg);
}

617
static bool wispr_manage_message(GWebResult *result,
618 619 620 621 622 623 624 625 626
			struct connman_wispr_portal_context *wp_context)
{
	DBG("Message type: %s (%d)",
		message_type_to_string(wp_context->wispr_msg.message_type),
					wp_context->wispr_msg.message_type);
	DBG("Response code: %s (%d)",
		response_code_to_string(wp_context->wispr_msg.response_code),
					wp_context->wispr_msg.response_code);

627
	if (wp_context->wispr_msg.access_procedure)
628 629
		DBG("Access procedure: %s",
			wp_context->wispr_msg.access_procedure);
630
	if (wp_context->wispr_msg.access_location)
631 632
		DBG("Access location: %s",
			wp_context->wispr_msg.access_location);
633
	if (wp_context->wispr_msg.location_name)
634 635
		DBG("Location name: %s",
			wp_context->wispr_msg.location_name);
636
	if (wp_context->wispr_msg.login_url)
637
		DBG("Login URL: %s", wp_context->wispr_msg.login_url);
638
	if (wp_context->wispr_msg.abort_login_url)
639 640
		DBG("Abort login URL: %s",
			wp_context->wispr_msg.abort_login_url);
641
	if (wp_context->wispr_msg.logoff_url)
642 643 644 645 646 647 648 649 650 651 652 653
		DBG("Logoff URL: %s", wp_context->wispr_msg.logoff_url);

	switch (wp_context->wispr_msg.message_type) {
	case 100:
		DBG("Login required");

		wp_context->wispr_result = CONNMAN_WISPR_RESULT_LOGIN;

		if (__connman_agent_request_login_input(wp_context->service,
					wispr_portal_request_wispr_login,
					wp_context) != -EINPROGRESS)
			wispr_portal_error(wp_context);
654
		else
655
			return true;
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673

		break;
	case 120: /* Falling down */
	case 140:
		if (wp_context->wispr_msg.response_code == 50) {
			wp_context->wispr_result = CONNMAN_WISPR_RESULT_ONLINE;

			g_free(wp_context->wispr_username);
			wp_context->wispr_username = NULL;

			g_free(wp_context->wispr_password);
			wp_context->wispr_password = NULL;

			g_free(wp_context->wispr_formdata);
			wp_context->wispr_formdata = NULL;

			wispr_portal_request_portal(wp_context);

674
			return true;
675 676 677 678 679 680 681 682
		} else
			wispr_portal_error(wp_context);

		break;
	default:
		break;
	}

683
	return false;
684 685
}

686
static bool wispr_portal_web_result(GWebResult *result, gpointer user_data)
687 688 689 690 691 692 693
{
	struct connman_wispr_portal_context *wp_context = user_data;
	const char *redirect = NULL;
	const guint8 *chunk = NULL;
	const char *str = NULL;
	guint16 status;
	gsize length;
694
	bool skip_failed = false;
695 696 697 698 699 700 701 702 703

	DBG("");

	if (wp_context->wispr_result != CONNMAN_WISPR_RESULT_ONLINE) {
		g_web_result_get_chunk(result, &chunk, &length);

		if (length > 0) {
			g_web_parser_feed_data(wp_context->wispr_parser,
								chunk, length);
704
			return true;
705 706 707 708
		}

		g_web_parser_end_data(wp_context->wispr_parser);

709 710 711 712
//		if (wp_context->wispr_msg.message_type >= 0) {
//			if (wispr_manage_message(result, wp_context))
//				goto done;
//		}
713 714 715 716 717 718 719 720
	}

	status = g_web_result_get_status(result);

	DBG("status: %03u", status);

	switch (status) {
	case 200:
721 722
//		if (wp_context->wispr_msg.message_type >= 0)
//			break;
723 724

		if (g_web_result_get_header(result, "X-ConnMan-Status",
725
						&str)) {
726 727
			// Cancel browser requests if useragent has not returned anything
			connman_agent_cancel(wp_context->service);
728
			portal_manage_status(result, wp_context);
729
			return false;
730
		} else
731 732 733 734
			__connman_agent_request_browser(wp_context->service,
					wispr_portal_browser_reply_cb,
					wp_context->redirect_url, wp_context);
		break;
Pasi Sjöholm's avatar
Pasi Sjöholm committed
735
	case 204:
736 737
		// Cancel browser requests if useragent has not returned anything
		connman_agent_cancel(wp_context->service);
Pasi Sjöholm's avatar
Pasi Sjöholm committed
738 739
		portal_manage_status(result, wp_context);
		return false;
740
	case 302:
741 742
	DBG("tls %d, Location header %d",(!g_web_supports_tls() ), (!g_web_result_get_header(result, "Location",
										&redirect)));
743 744 745
		if (!g_web_supports_tls() ||
			!g_web_result_get_header(result, "Location",
							&redirect)) {
746 747 748 749 750 751 752 753

			__connman_agent_request_browser(wp_context->service,
					wispr_portal_browser_reply_cb,
					wp_context->status_url, wp_context);
			break;
		}

		DBG("Redirect URL: %s", redirect);
754
		DBG("Status url URL: %s", wp_context->status_url);
755 756 757 758 759

		wp_context->redirect_url = g_strdup(redirect);
		wp_context->request_id = g_web_request_get(wp_context->web,
				redirect, wispr_portal_web_result,
				wispr_route_request, wp_context);
760
		skip_failed = true;
761

762 763 764 765 766 767
		break;
	case 000:
		DBG("Redirect URL: %s", redirect);
		DBG("Status url URL: %s", wp_context->status_url);

		__connman_agent_request_browser(wp_context->service,
Pasi Sjöholm's avatar
Pasi Sjöholm committed
768 769
										wispr_portal_browser_reply_cb,
										wp_context->status_url, wp_context);
770
		break;
771 772 773 774
	case 400:
	case 404:

		break;
775 776 777 778 779 780 781 782 783
	case 505:
		DBG("HTTP version not supported, handling over to the browser");
		DBG("Redirect URL: %s", redirect);
		DBG("Status url URL: %s", wp_context->status_url);

		__connman_agent_request_browser(wp_context->service,
					wispr_portal_browser_reply_cb,
					wp_context->status_url, wp_context);
		break;
784 785 786
	default:
		break;
	}
787
	if (!skip_failed && __connman_service_online_check_failed(wp_context->service,
788 789 790 791 792
		wp_context->type) == 0) {
		wispr_portal_error(wp_context);
		free_connman_wispr_portal_context(wp_context);
		return false;
	}
793 794 795 796

	free_wispr_routes(wp_context);
	wp_context->request_id = 0;
	wp_context->wispr_msg.message_type = -1;
797
	return false;
798 799 800 801 802 803 804 805
}

static void proxy_callback(const char *proxy, void *user_data)
{
	struct connman_wispr_portal_context *wp_context = user_data;

	DBG("proxy %s", proxy);

806
	if (!wp_context)
807 808 809 810
		return;

	wp_context->token = 0;

811 812 813 814 815
	if (proxy && g_strcmp0(proxy, "DIRECT") != 0) {
		if (g_str_has_prefix(proxy, "PROXY")) {
			proxy += 5;
			for (; *proxy == ' ' && *proxy != '\0'; proxy++);
		}
816
		g_web_set_proxy(wp_context->web, proxy);
817
	}
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863

	g_web_set_accept(wp_context->web, NULL);
	g_web_set_user_agent(wp_context->web, "ConnMan/%s wispr", VERSION);
	g_web_set_close_connection(wp_context->web, TRUE);

	connman_wispr_message_init(&wp_context->wispr_msg);

	wp_context->wispr_parser = g_web_parser_new(
					"<WISPAccessGatewayParam",
					"WISPAccessGatewayParam>",
					xml_wispr_parser_callback, wp_context);

	wispr_portal_request_portal(wp_context);
}

static gboolean no_proxy_callback(gpointer user_data)
{
	struct connman_wispr_portal_context *wp_context = user_data;

	wp_context->timeout = 0;

	proxy_callback("DIRECT", wp_context);

	return FALSE;
}

static int wispr_portal_detect(struct connman_wispr_portal_context *wp_context)
{
	enum connman_service_proxy_method proxy_method;
	enum connman_service_type service_type;
	char *interface = NULL;
	char **nameservers = NULL;
	int if_index;
	int err = 0;
	int i;

	DBG("wispr/portal context %p", wp_context);
	DBG("service %p", wp_context->service);

	service_type = connman_service_get_type(wp_context->service);

	switch (service_type) {
	case CONNMAN_SERVICE_TYPE_ETHERNET:
	case CONNMAN_SERVICE_TYPE_WIFI:
	case CONNMAN_SERVICE_TYPE_BLUETOOTH:
	case CONNMAN_SERVICE_TYPE_CELLULAR:
864
	case CONNMAN_SERVICE_TYPE_GADGET:
865 866 867 868 869
		break;
	case CONNMAN_SERVICE_TYPE_UNKNOWN:
	case CONNMAN_SERVICE_TYPE_SYSTEM:
	case CONNMAN_SERVICE_TYPE_GPS:
	case CONNMAN_SERVICE_TYPE_VPN:
870
	case CONNMAN_SERVICE_TYPE_P2P:
871 872 873 874
		return -EOPNOTSUPP;
	}

	interface = connman_service_get_interface(wp_context->service);
875
	if (!interface)
876 877 878 879 880 881 882 883 884 885 886 887
		return -EINVAL;

	DBG("interface %s", interface);

	if_index = connman_inet_ifindex(interface);
	if (if_index < 0) {
		DBG("Could not get ifindex");
		err = -EINVAL;
		goto done;
	}

	nameservers = connman_service_get_nameservers(wp_context->service);
888
	if (!nameservers) {
889 890 891 892 893 894
		DBG("Could not get nameservers");
		err = -EINVAL;
		goto done;
	}

	wp_context->web = g_web_new(if_index);
895
	if (!wp_context->web) {
896 897 898 899 900 901 902 903 904 905
		DBG("Could not set up GWeb");
		err = -ENOMEM;
		goto done;
	}

	if (getenv("CONNMAN_WEB_DEBUG"))
		g_web_set_debug(wp_context->web, web_debug, "WEB");

	if (wp_context->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
		g_web_set_address_family(wp_context->web, AF_INET);
Pasi Sjöholm's avatar
Pasi Sjöholm committed
906
		wp_context->status_url = connman_option_get_string(CONF_STATUS_URL_IPV4);
907 908
	} else {
		g_web_set_address_family(wp_context->web, AF_INET6);
Pasi Sjöholm's avatar
Pasi Sjöholm committed
909
		wp_context->status_url = connman_option_get_string(CONF_STATUS_URL_IPV6);
910 911
	}

912
	for (i = 0; nameservers[i]; i++)
913 914 915 916
		g_web_add_nameserver(wp_context->web, nameservers[i]);

	proxy_method = connman_service_get_proxy_method(wp_context->service);

917
	DBG("Proxy method %d",proxy_method);
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
	if (proxy_method != CONNMAN_SERVICE_PROXY_METHOD_DIRECT) {
		wp_context->token = connman_proxy_lookup(interface,
						wp_context->status_url,
						wp_context->service,
						proxy_callback, wp_context);

		if (wp_context->token == 0) {
			err = -EINVAL;
			free_connman_wispr_portal_context(wp_context);
		}
	} else if (wp_context->timeout == 0) {
		wp_context->timeout =
			g_timeout_add_seconds(0, no_proxy_callback, wp_context);
	}

done:
	g_strfreev(nameservers);

	g_free(interface);
	return err;
}

int __connman_wispr_start(struct connman_service *service,
					enum connman_ipconfig_type type)
{
	struct connman_wispr_portal_context *wp_context = NULL;
	struct connman_wispr_portal *wispr_portal = NULL;
	int index;

	DBG("service %p", service);

949
	if (!wispr_portal_list)
950 951 952 953 954 955 956 957
		return -EINVAL;

	index = __connman_service_get_index(service);
	if (index < 0)
		return -EINVAL;

	wispr_portal = g_hash_table_lookup(wispr_portal_list,
					GINT_TO_POINTER(index));
958
	if (!wispr_portal) {
959
		wispr_portal = g_try_new0(struct connman_wispr_portal, 1);
960
		if (!wispr_portal)
961 962 963 964 965 966 967 968 969 970 971 972 973 974
			return -ENOMEM;

		g_hash_table_replace(wispr_portal_list,
					GINT_TO_POINTER(index), wispr_portal);
	}

	if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
		wp_context = wispr_portal->ipv4_context;
	else if (type == CONNMAN_IPCONFIG_TYPE_IPV6)
		wp_context = wispr_portal->ipv6_context;
	else
		return -EINVAL;

	/* If there is already an existing context, we wipe it */
975
	if (wp_context)
976 977 978
		free_connman_wispr_portal_context(wp_context);

	wp_context = create_wispr_portal_context();
979
	if (!wp_context)
980 981
		return -ENOMEM;

982
	wp_context->service = connman_service_ref(service);
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
	wp_context->type = type;
	wp_context->wispr_portal = wispr_portal;

	if (type == CONNMAN_IPCONFIG_TYPE_IPV4)
		wispr_portal->ipv4_context = wp_context;
	else
		wispr_portal->ipv6_context = wp_context;

	return wispr_portal_detect(wp_context);
}

void __connman_wispr_stop(struct connman_service *service)
{
	int index;

	DBG("service %p", service);

1000
	if (!wispr_portal_list)
1001 1002 1003 1004 1005 1006
		return;

	index = __connman_service_get_index(service);
	if (index < 0)
		return;

1007
	connman_agent_cancel(service);
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
	g_hash_table_remove(wispr_portal_list, GINT_TO_POINTER(index));
}

int __connman_wispr_init(void)
{
	DBG("");

	wispr_portal_list = g_hash_table_new_full(g_direct_hash,
						g_direct_equal, NULL,
						free_connman_wispr_portal);

	return 0;
}

void __connman_wispr_cleanup(void)
{
	DBG("");

	g_hash_table_destroy(wispr_portal_list);
	wispr_portal_list = NULL;
}