136d97821Schristos /*
236d97821Schristos * Hotspot 2.0 client - Web browser using wpadebug on Android
336d97821Schristos * Copyright (c) 2013, Qualcomm Atheros, Inc.
436d97821Schristos *
536d97821Schristos * This software may be distributed under the terms of the BSD license.
636d97821Schristos * See README for more details.
736d97821Schristos */
836d97821Schristos
936d97821Schristos #include "includes.h"
1036d97821Schristos
1136d97821Schristos #include "common.h"
1236d97821Schristos #include "utils/eloop.h"
1336d97821Schristos #include "wps/http_server.h"
1436d97821Schristos #include "browser.h"
1536d97821Schristos
1636d97821Schristos
1736d97821Schristos struct browser_data {
1836d97821Schristos int success;
1936d97821Schristos };
2036d97821Schristos
2136d97821Schristos
browser_timeout(void * eloop_data,void * user_ctx)2236d97821Schristos static void browser_timeout(void *eloop_data, void *user_ctx)
2336d97821Schristos {
2436d97821Schristos wpa_printf(MSG_INFO, "Timeout on waiting browser interaction to "
2536d97821Schristos "complete");
2636d97821Schristos eloop_terminate();
2736d97821Schristos }
2836d97821Schristos
2936d97821Schristos
http_req(void * ctx,struct http_request * req)3036d97821Schristos static void http_req(void *ctx, struct http_request *req)
3136d97821Schristos {
3236d97821Schristos struct browser_data *data = ctx;
3336d97821Schristos struct wpabuf *resp;
3436d97821Schristos const char *url;
3536d97821Schristos int done = 0;
3636d97821Schristos
3736d97821Schristos url = http_request_get_uri(req);
3836d97821Schristos wpa_printf(MSG_INFO, "Browser response received: %s", url);
3936d97821Schristos
4036d97821Schristos if (os_strcmp(url, "/") == 0) {
4136d97821Schristos data->success = 1;
4236d97821Schristos done = 1;
4336d97821Schristos } else if (os_strncmp(url, "/osu/", 5) == 0) {
4436d97821Schristos data->success = atoi(url + 5);
4536d97821Schristos done = 1;
4636d97821Schristos }
4736d97821Schristos
4836d97821Schristos resp = wpabuf_alloc(100);
4936d97821Schristos if (resp == NULL) {
5036d97821Schristos http_request_deinit(req);
5136d97821Schristos if (done)
5236d97821Schristos eloop_terminate();
5336d97821Schristos return;
5436d97821Schristos }
55*ebb5671cSchristos wpabuf_put_str(resp, "HTTP/1.1\r\n\r\nUser input completed");
5636d97821Schristos
5736d97821Schristos if (done) {
5836d97821Schristos eloop_cancel_timeout(browser_timeout, NULL, NULL);
5936d97821Schristos eloop_register_timeout(0, 500000, browser_timeout, &data, NULL);
6036d97821Schristos }
6136d97821Schristos
6236d97821Schristos http_request_send_and_deinit(req, resp);
6336d97821Schristos }
6436d97821Schristos
6536d97821Schristos
hs20_web_browser(const char * url)6636d97821Schristos int hs20_web_browser(const char *url)
6736d97821Schristos {
6836d97821Schristos struct http_server *http;
6936d97821Schristos struct in_addr addr;
7036d97821Schristos struct browser_data data;
719a53cbbeSchristos pid_t pid;
7236d97821Schristos
7336d97821Schristos wpa_printf(MSG_INFO, "Launching wpadebug browser to %s", url);
7436d97821Schristos
7536d97821Schristos os_memset(&data, 0, sizeof(data));
7636d97821Schristos
7736d97821Schristos if (eloop_init() < 0) {
7836d97821Schristos wpa_printf(MSG_ERROR, "eloop_init failed");
7936d97821Schristos return -1;
8036d97821Schristos }
8136d97821Schristos addr.s_addr = htonl((127 << 24) | 1);
8236d97821Schristos http = http_server_init(&addr, 12345, http_req, &data);
8336d97821Schristos if (http == NULL) {
8436d97821Schristos wpa_printf(MSG_ERROR, "http_server_init failed");
8536d97821Schristos eloop_destroy();
8636d97821Schristos return -1;
8736d97821Schristos }
8836d97821Schristos
899a53cbbeSchristos pid = fork();
909a53cbbeSchristos if (pid < 0) {
919a53cbbeSchristos wpa_printf(MSG_ERROR, "fork: %s", strerror(errno));
9236d97821Schristos http_server_deinit(http);
9336d97821Schristos eloop_destroy();
9436d97821Schristos return -1;
9536d97821Schristos }
9636d97821Schristos
979a53cbbeSchristos if (pid == 0) {
989a53cbbeSchristos /* run the external command in the child process */
99928750b6Schristos char *argv[14];
100*ebb5671cSchristos char *envp[] = { "PATH=/system/bin:/vendor/bin", NULL };
1019a53cbbeSchristos
1029a53cbbeSchristos argv[0] = "browser-wpadebug";
1039a53cbbeSchristos argv[1] = "start";
1049a53cbbeSchristos argv[2] = "-a";
1059a53cbbeSchristos argv[3] = "android.action.MAIN";
1069a53cbbeSchristos argv[4] = "-c";
1079a53cbbeSchristos argv[5] = "android.intent.category.LAUNCHER";
1089a53cbbeSchristos argv[6] = "-n";
1099a53cbbeSchristos argv[7] = "w1.fi.wpadebug/.WpaWebViewActivity";
1109a53cbbeSchristos argv[8] = "-e";
1119a53cbbeSchristos argv[9] = "w1.fi.wpadebug.URL";
1129a53cbbeSchristos argv[10] = (void *) url;
113928750b6Schristos argv[11] = "--user";
114928750b6Schristos argv[12] = "-3"; /* USER_CURRENT_OR_SELF */
115928750b6Schristos argv[13] = NULL;
1169a53cbbeSchristos
117*ebb5671cSchristos execve("/system/bin/am", argv, envp);
118*ebb5671cSchristos wpa_printf(MSG_ERROR, "execve: %s", strerror(errno));
1199a53cbbeSchristos exit(0);
1209a53cbbeSchristos return -1;
1219a53cbbeSchristos }
1229a53cbbeSchristos
12336d97821Schristos eloop_register_timeout(300, 0, browser_timeout, &data, NULL);
12436d97821Schristos eloop_run();
12536d97821Schristos eloop_cancel_timeout(browser_timeout, &data, NULL);
12636d97821Schristos http_server_deinit(http);
12736d97821Schristos eloop_destroy();
12836d97821Schristos
12936d97821Schristos wpa_printf(MSG_INFO, "Closing Android browser");
13036d97821Schristos if (os_exec("/system/bin/am",
13136d97821Schristos "start -a android.action.MAIN "
13236d97821Schristos "-c android.intent.category.LAUNCHER "
13336d97821Schristos "-n w1.fi.wpadebug/.WpaWebViewActivity "
13436d97821Schristos "-e w1.fi.wpadebug.URL FINISH", 1) != 0) {
13536d97821Schristos wpa_printf(MSG_INFO, "Failed to close wpadebug browser");
13636d97821Schristos }
13736d97821Schristos
13836d97821Schristos return data.success;
13936d97821Schristos }
140