xref: /dflybsd-src/contrib/wpa_supplicant/src/utils/browser-wpadebug.c (revision 3a84a4273475ed07d0ab1c2dfeffdfedef35d9cd)
1*a1157835SDaniel Fojt /*
2*a1157835SDaniel Fojt  * Hotspot 2.0 client - Web browser using wpadebug on Android
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(100);
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 	wpabuf_put_str(resp, "HTTP/1.1\r\n\r\nUser input completed");
56*a1157835SDaniel Fojt 
57*a1157835SDaniel Fojt 	if (done) {
58*a1157835SDaniel Fojt 		eloop_cancel_timeout(browser_timeout, NULL, NULL);
59*a1157835SDaniel Fojt 		eloop_register_timeout(0, 500000, browser_timeout, &data, NULL);
60*a1157835SDaniel Fojt 	}
61*a1157835SDaniel Fojt 
62*a1157835SDaniel Fojt 	http_request_send_and_deinit(req, resp);
63*a1157835SDaniel Fojt }
64*a1157835SDaniel Fojt 
65*a1157835SDaniel Fojt 
hs20_web_browser(const char * url)66*a1157835SDaniel Fojt int hs20_web_browser(const char *url)
67*a1157835SDaniel Fojt {
68*a1157835SDaniel Fojt 	struct http_server *http;
69*a1157835SDaniel Fojt 	struct in_addr addr;
70*a1157835SDaniel Fojt 	struct browser_data data;
71*a1157835SDaniel Fojt 	pid_t pid;
72*a1157835SDaniel Fojt 
73*a1157835SDaniel Fojt 	wpa_printf(MSG_INFO, "Launching wpadebug browser to %s", url);
74*a1157835SDaniel Fojt 
75*a1157835SDaniel Fojt 	os_memset(&data, 0, sizeof(data));
76*a1157835SDaniel Fojt 
77*a1157835SDaniel Fojt 	if (eloop_init() < 0) {
78*a1157835SDaniel Fojt 		wpa_printf(MSG_ERROR, "eloop_init failed");
79*a1157835SDaniel Fojt 		return -1;
80*a1157835SDaniel Fojt 	}
81*a1157835SDaniel Fojt 	addr.s_addr = htonl((127 << 24) | 1);
82*a1157835SDaniel Fojt 	http = http_server_init(&addr, 12345, http_req, &data);
83*a1157835SDaniel Fojt 	if (http == NULL) {
84*a1157835SDaniel Fojt 		wpa_printf(MSG_ERROR, "http_server_init failed");
85*a1157835SDaniel Fojt 		eloop_destroy();
86*a1157835SDaniel Fojt 		return -1;
87*a1157835SDaniel Fojt 	}
88*a1157835SDaniel Fojt 
89*a1157835SDaniel Fojt 	pid = fork();
90*a1157835SDaniel Fojt 	if (pid < 0) {
91*a1157835SDaniel Fojt 		wpa_printf(MSG_ERROR, "fork: %s", strerror(errno));
92*a1157835SDaniel Fojt 		http_server_deinit(http);
93*a1157835SDaniel Fojt 		eloop_destroy();
94*a1157835SDaniel Fojt 		return -1;
95*a1157835SDaniel Fojt 	}
96*a1157835SDaniel Fojt 
97*a1157835SDaniel Fojt 	if (pid == 0) {
98*a1157835SDaniel Fojt 		/* run the external command in the child process */
99*a1157835SDaniel Fojt 		char *argv[14];
100*a1157835SDaniel Fojt 		char *envp[] = { "PATH=/system/bin:/vendor/bin", NULL };
101*a1157835SDaniel Fojt 
102*a1157835SDaniel Fojt 		argv[0] = "browser-wpadebug";
103*a1157835SDaniel Fojt 		argv[1] = "start";
104*a1157835SDaniel Fojt 		argv[2] = "-a";
105*a1157835SDaniel Fojt 		argv[3] = "android.action.MAIN";
106*a1157835SDaniel Fojt 		argv[4] = "-c";
107*a1157835SDaniel Fojt 		argv[5] = "android.intent.category.LAUNCHER";
108*a1157835SDaniel Fojt 		argv[6] = "-n";
109*a1157835SDaniel Fojt 		argv[7] = "w1.fi.wpadebug/.WpaWebViewActivity";
110*a1157835SDaniel Fojt 		argv[8] = "-e";
111*a1157835SDaniel Fojt 		argv[9] = "w1.fi.wpadebug.URL";
112*a1157835SDaniel Fojt 		argv[10] = (void *) url;
113*a1157835SDaniel Fojt 		argv[11] = "--user";
114*a1157835SDaniel Fojt 		argv[12] = "-3"; /* USER_CURRENT_OR_SELF */
115*a1157835SDaniel Fojt 		argv[13] = NULL;
116*a1157835SDaniel Fojt 
117*a1157835SDaniel Fojt 		execve("/system/bin/am", argv, envp);
118*a1157835SDaniel Fojt 		wpa_printf(MSG_ERROR, "execve: %s", strerror(errno));
119*a1157835SDaniel Fojt 		exit(0);
120*a1157835SDaniel Fojt 		return -1;
121*a1157835SDaniel Fojt 	}
122*a1157835SDaniel Fojt 
123*a1157835SDaniel Fojt 	eloop_register_timeout(300, 0, browser_timeout, &data, NULL);
124*a1157835SDaniel Fojt 	eloop_run();
125*a1157835SDaniel Fojt 	eloop_cancel_timeout(browser_timeout, &data, NULL);
126*a1157835SDaniel Fojt 	http_server_deinit(http);
127*a1157835SDaniel Fojt 	eloop_destroy();
128*a1157835SDaniel Fojt 
129*a1157835SDaniel Fojt 	wpa_printf(MSG_INFO, "Closing Android browser");
130*a1157835SDaniel Fojt 	if (os_exec("/system/bin/am",
131*a1157835SDaniel Fojt 		    "start -a android.action.MAIN "
132*a1157835SDaniel Fojt 		    "-c android.intent.category.LAUNCHER "
133*a1157835SDaniel Fojt 		    "-n w1.fi.wpadebug/.WpaWebViewActivity "
134*a1157835SDaniel Fojt 		    "-e w1.fi.wpadebug.URL FINISH", 1) != 0) {
135*a1157835SDaniel Fojt 		wpa_printf(MSG_INFO, "Failed to close wpadebug browser");
136*a1157835SDaniel Fojt 	}
137*a1157835SDaniel Fojt 
138*a1157835SDaniel Fojt 	return data.success;
139*a1157835SDaniel Fojt }
140