blob: dfb4b67977f876efb170b603d5017e667de4d989 [file] [log] [blame]
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -07001/*
2 * Hotspot 2.0 client - Web browser using wpadebug on Android
3 * Copyright (c) 2013, Qualcomm Atheros, Inc.
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "utils/eloop.h"
13#include "wps/http_server.h"
14#include "browser.h"
15
16
17struct browser_data {
18 int success;
19};
20
21
22static void browser_timeout(void *eloop_data, void *user_ctx)
23{
24 wpa_printf(MSG_INFO, "Timeout on waiting browser interaction to "
25 "complete");
26 eloop_terminate();
27}
28
29
30static void http_req(void *ctx, struct http_request *req)
31{
32 struct browser_data *data = ctx;
33 struct wpabuf *resp;
34 const char *url;
35 int done = 0;
36
37 url = http_request_get_uri(req);
38 wpa_printf(MSG_INFO, "Browser response received: %s", url);
39
40 if (os_strcmp(url, "/") == 0) {
41 data->success = 1;
42 done = 1;
43 } else if (os_strncmp(url, "/osu/", 5) == 0) {
44 data->success = atoi(url + 5);
45 done = 1;
46 }
47
48 resp = wpabuf_alloc(100);
49 if (resp == NULL) {
50 http_request_deinit(req);
51 if (done)
52 eloop_terminate();
53 return;
54 }
Dmitry Shmidtebd93af2017-02-21 13:40:44 -080055 wpabuf_put_str(resp, "HTTP/1.1\r\n\r\nUser input completed");
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -070056
57 if (done) {
58 eloop_cancel_timeout(browser_timeout, NULL, NULL);
59 eloop_register_timeout(0, 500000, browser_timeout, &data, NULL);
60 }
61
62 http_request_send_and_deinit(req, resp);
63}
64
65
66int hs20_web_browser(const char *url)
67{
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -070068 struct http_server *http;
69 struct in_addr addr;
70 struct browser_data data;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -080071 pid_t pid;
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -070072
73 wpa_printf(MSG_INFO, "Launching wpadebug browser to %s", url);
74
75 os_memset(&data, 0, sizeof(data));
76
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -070077 if (eloop_init() < 0) {
78 wpa_printf(MSG_ERROR, "eloop_init failed");
79 return -1;
80 }
81 addr.s_addr = htonl((127 << 24) | 1);
82 http = http_server_init(&addr, 12345, http_req, &data);
83 if (http == NULL) {
84 wpa_printf(MSG_ERROR, "http_server_init failed");
85 eloop_destroy();
86 return -1;
87 }
88
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -080089 pid = fork();
90 if (pid < 0) {
91 wpa_printf(MSG_ERROR, "fork: %s", strerror(errno));
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -070092 http_server_deinit(http);
93 eloop_destroy();
94 return -1;
95 }
96
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -080097 if (pid == 0) {
98 /* run the external command in the child process */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080099 char *argv[14];
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800100 char *envp[] = { "PATH=/system/bin:/vendor/bin", NULL };
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800101
102 argv[0] = "browser-wpadebug";
103 argv[1] = "start";
104 argv[2] = "-a";
105 argv[3] = "android.action.MAIN";
106 argv[4] = "-c";
107 argv[5] = "android.intent.category.LAUNCHER";
108 argv[6] = "-n";
109 argv[7] = "w1.fi.wpadebug/.WpaWebViewActivity";
110 argv[8] = "-e";
111 argv[9] = "w1.fi.wpadebug.URL";
112 argv[10] = (void *) url;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800113 argv[11] = "--user";
114 argv[12] = "-3"; /* USER_CURRENT_OR_SELF */
115 argv[13] = NULL;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800116
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800117 execve("/system/bin/am", argv, envp);
118 wpa_printf(MSG_ERROR, "execve: %s", strerror(errno));
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800119 exit(0);
120 return -1;
121 }
122
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700123 eloop_register_timeout(300, 0, browser_timeout, &data, NULL);
124 eloop_run();
125 eloop_cancel_timeout(browser_timeout, &data, NULL);
126 http_server_deinit(http);
127 eloop_destroy();
128
129 wpa_printf(MSG_INFO, "Closing Android browser");
Dmitry Shmidta7b06fa2014-10-09 12:56:52 -0700130 if (os_exec("/system/bin/am",
131 "start -a android.action.MAIN "
132 "-c android.intent.category.LAUNCHER "
133 "-n w1.fi.wpadebug/.WpaWebViewActivity "
134 "-e w1.fi.wpadebug.URL FINISH", 1) != 0) {
Dmitry Shmidtd5dc24e2014-03-12 14:22:04 -0700135 wpa_printf(MSG_INFO, "Failed to close wpadebug browser");
136 }
137
138 return data.success;
139}