forked from mnalis/android-wifi-upgrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_wifi.pl
executable file
·190 lines (164 loc) · 5.64 KB
/
convert_wifi.pl
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
#!/usr/bin/perl
# by Matija Nalis <[email protected]>, Apache 2.0 license, started 2019-12-02
#
# converts android WiFi passwords from old wpa_supplicant.conf to newer WifiConfigStore.xml
#
use warnings;
use strict;
use autodie;
use Data::Dumper;
use POSIX qw(strftime);
my $DEBUG = $ENV{DEBUG} || 0;
my $IGNORE_OPEN = $ENV{IGNORE_OPEN} || 0; # ignore open networks (without password) and do not convert them
my $CreationUID = $ENV{FORCEUID} || '1000'; # FIXME user configurable? or read from id_str (but it can be "-1" there!) or fix to "0" ?
$| = 1;
sub dbg($$) {
my ($lvl, $msg) = @_;
print STDERR "dbg:$lvl" . (' ' x ($lvl*1)) . "$msg\n" if $DEBUG >= $lvl;
}
my %CUR=();
# header of WifiConfigStore.xml
sub start_xml() {
print <<EOF
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<WifiConfigStoreData>
<int name="Version" value="1" />
<NetworkList>
EOF
}
# FIXME: we should never do XML by hand like this. oh well.
sub quote_xml($) {
my ($str) = @_;
$str =~ s{&}{&}g;
$str =~ s{<}{<}g;
$str =~ s{>}{>}g;
$str =~ s{\'}{'}g;
$str =~ s{\"}{"}g;
return $str;
}
#my $cnt = 0;
# constructs one network entry in WifiConfigStore.xml
sub add_xml() {
my $SSID = $CUR{ssid};
if (!defined $SSID) { return warn "Skipping - no SSID?! in " . Dumper(\%CUR) };
#if ($SSID !~ /^[a-zA-Z0-9_ \-\"\'\.<>&()@]+$/) { warn "Possibly problematic SSID name $SSID (FIXME needs escaping?)" };
my $key_mgmt = $CUR{key_mgmt} || ''; warn "no key_mgmt for SSID $SSID" if not defined $CUR{key_mgmt};
if ($CUR{key_mgmt} !~ /^NONE|WPA-PSK$/) { return warn "Skipping network with unknown key_mgmt=$key_mgmt for SSID $SSID" };
$key_mgmt =~ tr/-/_/;
if ($IGNORE_OPEN and $key_mgmt eq 'NONE') { return };
#return if $DEBUG > 0 and $cnt++ < 49; # FIXME DELME debug why it doesn't work will all 74 networks?
if (defined $CUR{auth_alg}) { warn "probably don't know how to correctly handle auth_alg=$CUR{auth_alg} in SSID $SSID" };
my $CreationTime = strftime "time=%m-%d %H:%M:%S.000", localtime; # FIXME example: time=12-02 01:47:38.625 -- year is lost??
my $PSK_LINE = '<null name="PreSharedKey" />';
my $AllowedKeyMgmt = '01'; # seems to be 01 for null PSK, 02 otherwise?
if ($key_mgmt ne 'NONE') {
if (!defined $CUR{psk}) { return warn "Skipping - no PSK for SSID $SSID" };
my $PreSharedKey = quote_xml $CUR{psk};
$AllowedKeyMgmt = '02';
$PSK_LINE = '<string name="PreSharedKey">' . $PreSharedKey . '</string>';
}
$SSID = quote_xml $SSID;
my $ConfigKey = "${SSID}$key_mgmt";
my $priority = $CUR{priority};
# output main config block with all variables filled-in
print qq{<Network>
<WifiConfiguration>
<string name="ConfigKey">$ConfigKey</string>
<string name="SSID">$SSID</string>
<null name="BSSID" />
$PSK_LINE
<null name="WEPKeys" />
<int name="WEPTxKeyIndex" value="0" />
<boolean name="HiddenSSID" value="false" />
<boolean name="RequirePMF" value="false" />
<byte-array name="AllowedKeyMgmt" num="1">$AllowedKeyMgmt</byte-array>
<byte-array name="AllowedProtocols" num="1">03</byte-array>
<byte-array name="AllowedAuthAlgos" num="1">01</byte-array>
<byte-array name="AllowedGroupCiphers" num="1">0f</byte-array>
<byte-array name="AllowedPairwiseCiphers" num="1">06</byte-array>
<boolean name="Shared" value="true" />
<int name="Status" value="2" />
<null name="FQDN" />
<null name="ProviderFriendlyName" />
<null name="LinkedNetworksList" />
<null name="DefaultGwMacAddress" />
<boolean name="ValidatedInternetAccess" value="false" />
<boolean name="NoInternetAccessExpected" value="false" />
<int name="UserApproved" value="0" />
<boolean name="MeteredHint" value="false" />
<int name="MeteredOverride" value="0" />
<boolean name="UseExternalScores" value="false" />
<int name="NumAssociation" value="$priority" />
<int name="CreatorUid" value="$CreationUID" />
<string name="CreatorName">android.uid.system:$CreationUID</string>
<string name="CreationTime">$CreationTime</string>
<int name="LastUpdateUid" value="$CreationUID" />
<string name="LastUpdateName">android.uid.system:$CreationUID</string>
<int name="LastConnectUid" value="0" />
<boolean name="IsLegacyPasspointConfig" value="false" />
<long-array name="RoamingConsortiumOIs" num="0" />
</WifiConfiguration>
<NetworkStatus>
<string name="SelectionStatus">NETWORK_SELECTION_ENABLED</string>
<string name="DisableReason">NETWORK_SELECTION_ENABLE</string>
<null name="ConnectChoice" />
<long name="ConnectChoiceTimeStamp" value="-1" />
<boolean name="HasEverConnected" value="false" />
</NetworkStatus>
<IpConfiguration>
<string name="IpAssignment">DHCP</string>
<string name="ProxySettings">NONE</string>
</IpConfiguration>
</Network>
};
#die "FIXME TEST KRAJ";
# FIXME original looks like this:
#network={
# ssid="SomeNet name"
# bssid=a4:1d:6b:4b:3e:2f
# psk="SomePassword"
# key_mgmt=WPA-PSK
# priority=201
# disabled=1
# id_str="%7B%22creatorUid%22%3A%22-1%22%2C%22configKey%22%3A%22%5C%22SomeNet+name%5C%22WPA_PSK%22%7D"
#}
}
# footer of WifiConfigStore.xml
sub end_xml() {
print <<EOF
</NetworkList>
<PasspointConfigData>
<long name="ProviderIndex" value="0" />
</PasspointConfigData>
</WifiConfigStoreData>
EOF
}
#
# here goes the main loop
#
start_xml();
while (<STDIN>)
{
chomp;
dbg 9, "line: $_";
next if /^\s*$/;
if (/^\s*network\s*=\s*{/) { # "network={" begins a new block
%CUR=();
next;
}
if (/^\s*}/) { # sole "}" ends the block
dbg 5, "block end: " . Dumper(\%CUR);
if (!defined $CUR{'ssid'}) {
warn "can't parse network without SSID, ignoring block:" . Dumper(\%CUR);
}
add_xml();
%CUR=();
next;
}
if (/^\s*(\w+)\s*=\s*(.*)$/) { # key=value pairs
$CUR{lc($1)}=$2;
} else {
warn "ignoring unparseable line: $_";
}
}
end_xml();