aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/fil/libre/repwifiapp/helpers/Engine6p0.java
blob: d4ff0f784d49b306aa5e5699f5a90b491b08e84b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
//
// Copyright 2017 Filippo "Fil" Bergamo <fil.bergamo@riseup.net>
// 
// This file is part of RepWifiApp.
//
// RepWifiApp is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// RepWifiApp is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with RepWifiApp.  If not, see <http://www.gnu.org/licenses/>.
// 
// ********************************************************************

package fil.libre.repwifiapp.helpers;

import org.apache.http.conn.util.InetAddressUtils;
import fil.libre.repwifiapp.Commons;

public class Engine6p0 extends Engine {

    @Override
    protected String getCmdWpaStart() {
        return "wpa_supplicant -B -dd -i" + Commons.INTERFACE_NAME + " -C" + Commons.SOCKET_DIR
                        + " -P" + Commons.PID_FILE + " -I" + Commons.OVERLAY_FILE + " -e"
                        + Commons.ENTROPY_FILE;
    }

    @Override
    public boolean connect(AccessPointInfo info) {

        killBackEndProcesses();

        if (info == null) {
            Utils.logDebug("Engine's connect() received a null AccessPointInfo");
            return false;
        }

        // clear any previously set network
        if (!destroyNetwork()) {
            Utils.logDebug("Unable to ndc destroy network");
            return false;
        }

        // clear interface's ip
        if (!clearAddrs()) {
            Utils.logDebug("Unable to ndc clearaddrs");
            return false;
        }

        // bring up interface
        if (!interfaceUp()) {
            Utils.logDebug("Unable to bring up interface.");
            return false;
        }

        // launch wpa_supplicant specifying our custom configuration and the
        // socket file
        if (!executeRootCmd(getCmdWpaStart())) {
            Utils.logDebug("Unable to run wpa start");
            return false;
        }

        // create new network and get network id
        String netID = createNetworkGetId();
        if (netID == null) {
            Utils.logDebug("Unable to fetch network id");
            return false;
        }

        // set network SSID
        if (!setNetworkSSID(info.getSsid(), netID)) {
            Utils.logDebug("Failed to set network ssid");
            return false;
        }

        if (info.isHidden() && !setNetworkScanSSID(netID)) {
            Utils.logDebug("Failed to set scan_ssid 1 for hidden network.");
            return false;
        }

        // set password (if any)
        if (!setNetworkPSK(info, netID)) {
            Utils.logDebug("Failed to set network psk");
            return false;
        }

        // select the network we just created
        if (!selectNetwork(netID)) {
            Utils.logDebug("Unable to wpa_cli select network");
            return false;
        }

        // enable the newtork
        if (!enableNetwork(netID)) {
            Utils.logDebug("Unable to wpa_cli enable_newtork");
            return false;
        }

        // try to reassociate to Access Point
        /*
         * if (! reassociate()){
         * Utils.logDebug("Unable to wpa_cli reassociate"); return false; }
         */

        // get DHCP
        Utils.logDebug("Attempt to run dhcpcd..");
        if (!runDhcpcd()) {
            Utils.logDebug("Failed to run dhcpcd");
            return false;
        }

        // try to fetch gateway
        String gw = getGateway();
        if (gw == null || gw.trim().length() < 7) {
            // failed to get gateway
            Utils.logDebug("Failed to get gateway");
            return false;
        }

        if (!executeRootCmd("ndc network create 1")) {
            Utils.logDebug("Failed to wpa_cli network create 1 ");
            return false;
        }

        if (!executeRootCmd("ndc network interface add 1 " + Commons.INTERFACE_NAME)) {
            Utils.logDebug("Failed to add interface.");
            return false;
        }

        // set route to gateway for all traffic
        if (!executeRootCmd("ndc network route add 1 " + Commons.INTERFACE_NAME + " 0.0.0.0/0 "
                        + gw)) {
            Utils.logDebug("Failed to add route to gateway");
            return false;
        }

        if (!setDns(Commons.getDnss(), gw)) {
            Utils.logDebug("Failed to set DNS");
            return false;
        }

        // use network
        if (!executeRootCmd("ndc network default set 1")) {
            Utils.logDebug("Failed to set network as default");
            return false;
        }

        return true;

    }

    private String createNetworkGetId() {

        try {

            RootCommand su = new RootCommand(getCmdWpaCli() + " add_network");
            if (su.execute() == 0) {
                String out = su.getOutput();
                if (out == null || out.trim().equals("")) {
                    return null;
                } else {
                    return out.replace("\n", "");
                }
            } else {
                return null;
            }

        } catch (Exception e) {
            Utils.logError("Error while creating network", e);
            return null;
        }

    }

    private boolean destroyNetwork() {
        // needs root (tested)
        return executeRootCmd("ndc network destroy 1");
    }

    private boolean setNetworkSSID(String ssid, String networkID) {

        try {

            // needs root (wpa_cli)
            RootCommand su = new RootCommand(getCmdWpaCli() + " set_network " + networkID
                            + " ssid '\"" + ssid + "\"'");
            if (su.execute() == 0) {
                String out = su.getOutput();
                if (out != null && out.trim().replace("\n", "").equals("OK")) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }

        } catch (Exception e) {
            Utils.logError("Error while setting network SSID", e);
            return false;
        }

    }

    private boolean setNetworkPSK(AccessPointInfo info, String networkID) {

        try {

            // needs root (wpa_cli)

            String cmdSetPass = null;
            if (info.needsPassword()) {
                cmdSetPass = getCmdWpaCli() + " set_network " + networkID + " psk '\""
                                + info.getPassword() + "\"'";
            } else {
                cmdSetPass = getCmdWpaCli() + " set_network " + networkID + " key_mgmt NONE";
            }

            RootCommand su = new RootCommand(cmdSetPass);
            if (su.execute() == 0) {
                String out = su.getOutput();
                if (out != null && out.trim().replace("\n", "").equals("OK")) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }

        } catch (Exception e) {
            Utils.logError("Error while setting network PSK", e);
            return false;
        }

    }

    private boolean setNetworkScanSSID(String networkID) {

        try {

            // needs root (wpa_cli)
            RootCommand su = new RootCommand(getCmdWpaCli() + " set_network " + networkID
                            + " scan_ssid 1");
            if (su.execute() == 0) {
                String out = su.getOutput();
                if (out != null && out.trim().replace("\n", "").equals("OK")) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }

        } catch (Exception e) {
            Utils.logError("Error while setting network SSID", e);
            return false;
        }
    }

    private boolean selectNetwork(String networkID) {

        try {

            // needs root (wpa_cli)
            RootCommand su = new RootCommand(getCmdWpaCli() + " select_network " + networkID);
            if (su.execute() == 0) {
                String out = su.getOutput();
                if (out != null && out.trim().replace("\n", "").equals("OK")) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }

        } catch (Exception e) {
            Utils.logError("Error while selecting network", e);
            return false;
        }

    }

    private boolean enableNetwork(String networkID) {

        try {

            // needs root (wpa_cli)

            RootCommand su = new RootCommand(getCmdWpaCli() + " enable_network " + networkID);
            if (su.execute() == 0) {
                String out = su.getOutput();
                if (out != null && out.trim().replace("\n", "").equals("OK")) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }

        } catch (Exception e) {
            Utils.logError("Error while enabling network", e);
            return false;
        }

    }

    private boolean setDns(String[] dnss, String gateway) {

        if (dnss == null || dnss.length == 0) {
            // the DNS setting has been left blank
            // try to use the gateway as dns

            if (gateway == null || gateway.length() == 0) {
                // no possible DNS.
                return false;
            }

            dnss = new String[] { gateway, gateway };

        }

        if (!InetAddressUtils.isIPv4Address(dnss[0])) {
            // invalid ip can't proceed.
            return false;
        }

        String cmd = "ndc resolver setnetdns 1 " + dnss[0];

        if (dnss.length > 1 && InetAddressUtils.isIPv4Address(dnss[1])) {
            cmd += " " + dnss[1];
        } else {
            cmd += " " + dnss[0];
        }

        return executeRootCmd(cmd);
    }

    private String getGateway() {

        try {

            // doesn't need root (tested)
            RootCommand cmd = new RootCommand("ip route show dev " + Commons.INTERFACE_NAME);
            if (cmd.execute() != 0) {
                Utils.logDebug("command failed show route");
                return null;
            }

            // read command output
            String out = cmd.getOutput();
            if (out == null) {
                return null;
            }

            String[] lines = out.split("\n");

            for (String l : lines) {

                if (l.contains("default via")) {

                    String[] f = l.split(" ");
                    if (f.length > 2) {

                        // found route's address:
                        return f[2];

                    }
                }
            }

            return null;

        } catch (Exception e) {
            Utils.logError("Error while trying to fetch route", e);
            return null;
        }

    }

    private boolean clearAddrs() {
        // needs root (tested)
        return executeRootCmd("ndc interface clearaddrs " + Commons.INTERFACE_NAME);
    }

}