xref: /dflybsd-src/contrib/wpa_supplicant/src/utils/browser-system.c (revision 3a84a4273475ed07d0ab1c2dfeffdfedef35d9cd)
1*a1157835SDaniel Fojt /*
2*a1157835SDaniel Fojt  * Hotspot 2.0 client - Web browser using system browser
3*a1157835SDaniel Fojt  * Copyright (c) 2013, Qualcomm Atheros, Inc.
4*a1157835SDaniel Fojt  *
5*a1157835SDaniel Fojt  * This software may be distributed under the terms of the BSD license.
6*a1157835SDaniel Fojt  * See README for more details.
7*a1157835SDaniel Fojt  */
8*a1157835SDaniel Fojt 
9*a1157835SDaniel Fojt #include "includes.h"
10*a1157835SDaniel Fojt 
11*a1157835SDaniel Fojt #include "common.h"
12*a1157835SDaniel Fojt #include "utils/eloop.h"
13*a1157835SDaniel Fojt #include "wps/http_server.h"
14*a1157835SDaniel Fojt #include "browser.h"
15*a1157835SDaniel Fojt 
16*a1157835SDaniel Fojt 
17*a1157835SDaniel Fojt struct browser_data {
18*a1157835SDaniel Fojt 	int success;
19*a1157835SDaniel Fojt };
20*a1157835SDaniel Fojt 
21*a1157835SDaniel Fojt 
browser_timeout(void * eloop_data,void * user_ctx)22*a1157835SDaniel Fojt static void browser_timeout(void *eloop_data, void *user_ctx)
23*a1157835SDaniel Fojt {
24*a1157835SDaniel Fojt 	wpa_printf(MSG_INFO, "Timeout on waiting browser interaction to "
25*a1157835SDaniel Fojt 		   "complete");
26*a1157835SDaniel Fojt 	eloop_terminate();
27*a1157835SDaniel Fojt }
28*a1157835SDaniel Fojt 
29*a1157835SDaniel Fojt 
http_req(void * ctx,struct http_request * req)30*a1157835SDaniel Fojt static void http_req(void *ctx, struct http_request *req)
31*a1157835SDaniel Fojt {
32*a1157835SDaniel Fojt 	struct browser_data *data = ctx;
33*a1157835SDaniel Fojt 	struct wpabuf *resp;
34*a1157835SDaniel Fojt 	const char *url;
35*a1157835SDaniel Fojt 	int done = 0;
36*a1157835SDaniel Fojt 
37*a1157835SDaniel Fojt 	url = http_request_get_uri(req);
38*a1157835SDaniel Fojt 	wpa_printf(MSG_INFO, "Browser response received: %s", url);
39*a1157835SDaniel Fojt 
40*a1157835SDaniel Fojt 	if (os_strcmp(url, "/") == 0) {
41*a1157835SDaniel Fojt 		data->success = 1;
42*a1157835SDaniel Fojt 		done = 1;
43*a1157835SDaniel Fojt 	} else if (os_strncmp(url, "/osu/", 5) == 0) {
44*a1157835SDaniel Fojt 		data->success = atoi(url + 5);
45*a1157835SDaniel Fojt 		done = 1;
46*a1157835SDaniel Fojt 	}
47*a1157835SDaniel Fojt 
48*a1157835SDaniel Fojt 	resp = wpabuf_alloc(1);
49*a1157835SDaniel Fojt 	if (resp == NULL) {
50*a1157835SDaniel Fojt 		http_request_deinit(req);
51*a1157835SDaniel Fojt 		if (done)
52*a1157835SDaniel Fojt 			eloop_terminate();
53*a1157835SDaniel Fojt 		return;
54*a1157835SDaniel Fojt 	}
55*a1157835SDaniel Fojt 
56*a1157835SDaniel Fojt 	if (done) {
57*a1157835SDaniel Fojt 		eloop_cancel_timeout(browser_timeout, NULL, NULL);
58*a1157835SDaniel Fojt 		eloop_register_timeout(0, 500000, browser_timeout, &data, NULL);
59*a1157835SDaniel Fojt 	}
60*a1157835SDaniel Fojt 
61*a1157835SDaniel Fojt 	http_request_send_and_deinit(req, resp);
62*a1157835SDaniel Fojt }
63*a1157835SDaniel Fojt 
64*a1157835SDaniel Fojt 
hs20_web_browser(const char * url)65*a1157835SDaniel Fojt int hs20_web_browser(const char *url)
66*a1157835SDaniel Fojt {
67*a1157835SDaniel Fojt 	struct http_server *http;
68*a1157835SDaniel Fojt 	struct in_addr addr;
69*a1157835SDaniel Fojt 	struct browser_data data;
70*a1157835SDaniel Fojt 	pid_t pid;
71*a1157835SDaniel Fojt 
72*a1157835SDaniel Fojt 	wpa_printf(MSG_INFO, "Launching system browser to %s", url);
73*a1157835SDaniel Fojt 
74*a1157835SDaniel Fojt 	os_memset(&data, 0, sizeof(data));
75*a1157835SDaniel Fojt 
76*a1157835SDaniel Fojt 	if (eloop_init() < 0) {
77*a1157835SDaniel Fojt 		wpa_printf(MSG_ERROR, "eloop_init failed");
78*a1157835SDaniel Fojt 		return -1;
79*a1157835SDaniel Fojt 	}
80*a1157835SDaniel Fojt 	addr.s_addr = htonl((127 << 24) | 1);
81*a1157835SDaniel Fojt 	http = http_server_init(&addr, 12345, http_req, &data);
82*a1157835SDaniel Fojt 	if (http == NULL) {
83*a1157835SDaniel Fojt 		wpa_printf(MSG_ERROR, "http_server_init failed");
84*a1157835SDaniel Fojt 		eloop_destroy();
85*a1157835SDaniel Fojt 		return -1;
86*a1157835SDaniel Fojt 	}
87*a1157835SDaniel Fojt 
88*a1157835SDaniel Fojt 	pid = fork();
89*a1157835SDaniel Fojt 	if (pid < 0) {
90*a1157835SDaniel Fojt 		wpa_printf(MSG_ERROR, "fork: %s", strerror(errno));
91*a1157835SDaniel Fojt 		http_server_deinit(http);
92*a1157835SDaniel Fojt 		eloop_destroy();
93*a1157835SDaniel Fojt 		return -1;
94*a1157835SDaniel Fojt 	}
95*a1157835SDaniel Fojt 
96*a1157835SDaniel Fojt 	if (pid == 0) {
97*a1157835SDaniel Fojt 		/* run the external command in the child process */
98*a1157835SDaniel Fojt 		char *argv[3];
99*a1157835SDaniel Fojt 
100*a1157835SDaniel Fojt 		argv[0] = "browser-system";
101*a1157835SDaniel Fojt 		argv[1] = (void *) url;
102*a1157835SDaniel Fojt 		argv[2] = NULL;
103*a1157835SDaniel Fojt 
104*a1157835SDaniel Fojt 		execv("/usr/bin/x-www-browser", argv);
105*a1157835SDaniel Fojt 		wpa_printf(MSG_ERROR, "execv: %s", strerror(errno));
106*a1157835SDaniel Fojt 		exit(0);
107*a1157835SDaniel Fojt 		return -1;
108*a1157835SDaniel Fojt 	}
109*a1157835SDaniel Fojt 
110*a1157835SDaniel Fojt 	eloop_register_timeout(120, 0, browser_timeout, &data, NULL);
111*a1157835SDaniel Fojt 	eloop_run();
112*a1157835SDaniel Fojt 	eloop_cancel_timeout(browser_timeout, &data, NULL);
113*a1157835SDaniel Fojt 	http_server_deinit(http);
114*a1157835SDaniel Fojt 	eloop_destroy();
115*a1157835SDaniel Fojt 
116*a1157835SDaniel Fojt 	/* TODO: Close browser */
117*a1157835SDaniel Fojt 
118*a1157835SDaniel Fojt 	return data.success;
119*a1157835SDaniel Fojt }
120