xref: /freebsd/contrib/wpa/src/utils/browser-wpadebug.c (revision c1d255d3ffdbe447de3ab875bf4e7d7accc5bfc5)
15b9c547cSRui Paulo /*
25b9c547cSRui Paulo  * Hotspot 2.0 client - Web browser using wpadebug on Android
35b9c547cSRui Paulo  * Copyright (c) 2013, Qualcomm Atheros, Inc.
45b9c547cSRui Paulo  *
55b9c547cSRui Paulo  * This software may be distributed under the terms of the BSD license.
65b9c547cSRui Paulo  * See README for more details.
75b9c547cSRui Paulo  */
85b9c547cSRui Paulo 
95b9c547cSRui Paulo #include "includes.h"
105b9c547cSRui Paulo 
115b9c547cSRui Paulo #include "common.h"
125b9c547cSRui Paulo #include "utils/eloop.h"
135b9c547cSRui Paulo #include "wps/http_server.h"
145b9c547cSRui Paulo #include "browser.h"
155b9c547cSRui Paulo 
165b9c547cSRui Paulo 
175b9c547cSRui Paulo struct browser_data {
185b9c547cSRui Paulo 	int success;
195b9c547cSRui Paulo };
205b9c547cSRui Paulo 
215b9c547cSRui Paulo 
browser_timeout(void * eloop_data,void * user_ctx)225b9c547cSRui Paulo static void browser_timeout(void *eloop_data, void *user_ctx)
235b9c547cSRui Paulo {
245b9c547cSRui Paulo 	wpa_printf(MSG_INFO, "Timeout on waiting browser interaction to "
255b9c547cSRui Paulo 		   "complete");
265b9c547cSRui Paulo 	eloop_terminate();
275b9c547cSRui Paulo }
285b9c547cSRui Paulo 
295b9c547cSRui Paulo 
http_req(void * ctx,struct http_request * req)305b9c547cSRui Paulo static void http_req(void *ctx, struct http_request *req)
315b9c547cSRui Paulo {
325b9c547cSRui Paulo 	struct browser_data *data = ctx;
335b9c547cSRui Paulo 	struct wpabuf *resp;
345b9c547cSRui Paulo 	const char *url;
355b9c547cSRui Paulo 	int done = 0;
365b9c547cSRui Paulo 
375b9c547cSRui Paulo 	url = http_request_get_uri(req);
385b9c547cSRui Paulo 	wpa_printf(MSG_INFO, "Browser response received: %s", url);
395b9c547cSRui Paulo 
405b9c547cSRui Paulo 	if (os_strcmp(url, "/") == 0) {
415b9c547cSRui Paulo 		data->success = 1;
425b9c547cSRui Paulo 		done = 1;
435b9c547cSRui Paulo 	} else if (os_strncmp(url, "/osu/", 5) == 0) {
445b9c547cSRui Paulo 		data->success = atoi(url + 5);
455b9c547cSRui Paulo 		done = 1;
465b9c547cSRui Paulo 	}
475b9c547cSRui Paulo 
485b9c547cSRui Paulo 	resp = wpabuf_alloc(100);
495b9c547cSRui Paulo 	if (resp == NULL) {
505b9c547cSRui Paulo 		http_request_deinit(req);
515b9c547cSRui Paulo 		if (done)
525b9c547cSRui Paulo 			eloop_terminate();
535b9c547cSRui Paulo 		return;
545b9c547cSRui Paulo 	}
5585732ac8SCy Schubert 	wpabuf_put_str(resp, "HTTP/1.1\r\n\r\nUser input completed");
565b9c547cSRui Paulo 
575b9c547cSRui Paulo 	if (done) {
585b9c547cSRui Paulo 		eloop_cancel_timeout(browser_timeout, NULL, NULL);
595b9c547cSRui Paulo 		eloop_register_timeout(0, 500000, browser_timeout, &data, NULL);
605b9c547cSRui Paulo 	}
615b9c547cSRui Paulo 
625b9c547cSRui Paulo 	http_request_send_and_deinit(req, resp);
635b9c547cSRui Paulo }
645b9c547cSRui Paulo 
655b9c547cSRui Paulo 
hs20_web_browser(const char * url,int ignore_tls)66*c1d255d3SCy Schubert int hs20_web_browser(const char *url, int ignore_tls)
675b9c547cSRui Paulo {
685b9c547cSRui Paulo 	struct http_server *http;
695b9c547cSRui Paulo 	struct in_addr addr;
705b9c547cSRui Paulo 	struct browser_data data;
715b9c547cSRui Paulo 	pid_t pid;
725b9c547cSRui Paulo 
735b9c547cSRui Paulo 	wpa_printf(MSG_INFO, "Launching wpadebug browser to %s", url);
745b9c547cSRui Paulo 
755b9c547cSRui Paulo 	os_memset(&data, 0, sizeof(data));
765b9c547cSRui Paulo 
775b9c547cSRui Paulo 	if (eloop_init() < 0) {
785b9c547cSRui Paulo 		wpa_printf(MSG_ERROR, "eloop_init failed");
795b9c547cSRui Paulo 		return -1;
805b9c547cSRui Paulo 	}
815b9c547cSRui Paulo 	addr.s_addr = htonl((127 << 24) | 1);
825b9c547cSRui Paulo 	http = http_server_init(&addr, 12345, http_req, &data);
835b9c547cSRui Paulo 	if (http == NULL) {
845b9c547cSRui Paulo 		wpa_printf(MSG_ERROR, "http_server_init failed");
855b9c547cSRui Paulo 		eloop_destroy();
865b9c547cSRui Paulo 		return -1;
875b9c547cSRui Paulo 	}
885b9c547cSRui Paulo 
895b9c547cSRui Paulo 	pid = fork();
905b9c547cSRui Paulo 	if (pid < 0) {
915b9c547cSRui Paulo 		wpa_printf(MSG_ERROR, "fork: %s", strerror(errno));
925b9c547cSRui Paulo 		http_server_deinit(http);
935b9c547cSRui Paulo 		eloop_destroy();
945b9c547cSRui Paulo 		return -1;
955b9c547cSRui Paulo 	}
965b9c547cSRui Paulo 
975b9c547cSRui Paulo 	if (pid == 0) {
985b9c547cSRui Paulo 		/* run the external command in the child process */
99325151a3SRui Paulo 		char *argv[14];
10085732ac8SCy Schubert 		char *envp[] = { "PATH=/system/bin:/vendor/bin", NULL };
1015b9c547cSRui Paulo 
1025b9c547cSRui Paulo 		argv[0] = "browser-wpadebug";
1035b9c547cSRui Paulo 		argv[1] = "start";
1045b9c547cSRui Paulo 		argv[2] = "-a";
1055b9c547cSRui Paulo 		argv[3] = "android.action.MAIN";
1065b9c547cSRui Paulo 		argv[4] = "-c";
1075b9c547cSRui Paulo 		argv[5] = "android.intent.category.LAUNCHER";
1085b9c547cSRui Paulo 		argv[6] = "-n";
1095b9c547cSRui Paulo 		argv[7] = "w1.fi.wpadebug/.WpaWebViewActivity";
1105b9c547cSRui Paulo 		argv[8] = "-e";
1115b9c547cSRui Paulo 		argv[9] = "w1.fi.wpadebug.URL";
1125b9c547cSRui Paulo 		argv[10] = (void *) url;
113325151a3SRui Paulo 		argv[11] = "--user";
114325151a3SRui Paulo 		argv[12] = "-3"; /* USER_CURRENT_OR_SELF */
115325151a3SRui Paulo 		argv[13] = NULL;
1165b9c547cSRui Paulo 
11785732ac8SCy Schubert 		execve("/system/bin/am", argv, envp);
11885732ac8SCy Schubert 		wpa_printf(MSG_ERROR, "execve: %s", strerror(errno));
1195b9c547cSRui Paulo 		exit(0);
1205b9c547cSRui Paulo 		return -1;
1215b9c547cSRui Paulo 	}
1225b9c547cSRui Paulo 
1235b9c547cSRui Paulo 	eloop_register_timeout(300, 0, browser_timeout, &data, NULL);
1245b9c547cSRui Paulo 	eloop_run();
1255b9c547cSRui Paulo 	eloop_cancel_timeout(browser_timeout, &data, NULL);
1265b9c547cSRui Paulo 	http_server_deinit(http);
1275b9c547cSRui Paulo 	eloop_destroy();
1285b9c547cSRui Paulo 
1295b9c547cSRui Paulo 	wpa_printf(MSG_INFO, "Closing Android browser");
1305b9c547cSRui Paulo 	if (os_exec("/system/bin/am",
1315b9c547cSRui Paulo 		    "start -a android.action.MAIN "
1325b9c547cSRui Paulo 		    "-c android.intent.category.LAUNCHER "
1335b9c547cSRui Paulo 		    "-n w1.fi.wpadebug/.WpaWebViewActivity "
1345b9c547cSRui Paulo 		    "-e w1.fi.wpadebug.URL FINISH", 1) != 0) {
1355b9c547cSRui Paulo 		wpa_printf(MSG_INFO, "Failed to close wpadebug browser");
1365b9c547cSRui Paulo 	}
1375b9c547cSRui Paulo 
1385b9c547cSRui Paulo 	return data.success;
1395b9c547cSRui Paulo }
140