-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsync-wifi
executable file
·275 lines (255 loc) · 7.01 KB
/
sync-wifi
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
#!/usr/bin/perl
use strict;
use warnings;
sub addWifiFromNet();
sub clearAllWifi();
sub getExisting();
sub getWifiGconf($$$$);
sub getGconfCmds($);
sub gconfGetCmd($);
sub gconfSetCmd($$$;$);
sub listInt($);
sub listString($);
sub ssidListInt($);
sub wpaPskListInt($$);
sub randHex($);
sub randomId();
sub runCmdN9(@);
sub runBT(@);
my $usage = "Usage:
$0 -g
print all wifi networks on the phone
$0
remove all wifi networks from the phone and replace with net
";
my $rootDir = '/system/osso/connectivity/IAP';
my $noAutoFile = "$ENV{HOME}/Code/n9/wifi-noauto";
my $TRUE = ['true', 'bool'];
my $FALSE = ['false', 'bool'];
my $wifiAtts = {
name => [""],
wlan_ssid => listInt [],
EAP_wpa_preshared_key => undef,
EAP_wpa_preshared_passphrase => undef,
wlan_wepkey1 => undef,
http_check_status => ["no_login"],
proxytype => ["NONE"],
wlan_hidden => $FALSE,
wlan_security => undef,
type => ["WLAN_INFRA"],
autoconnect => $TRUE,
ipv4_type => ["AUTO"],
};
my $gprsAtts = {
name => [""],
gprs_accesspointname => [""],
sim_imsi => [""],
gprs_username => [""],
gprs_password => [""],
type => ["GPRS"],
autoconnect => $TRUE,
ask_password => $FALSE,
first_time_dialog_shown => $TRUE,
ipv4_autodns => $TRUE,
ipv4_type => ["AUTO"],
};
sub main(@){
my $get;
if(@_ == 1 and $_[0] =~ /^(-g)$/){
$get = shift;
}
die $usage if @_ > 0;
if(defined $get){
my $networks = getExisting();
my $sortByName = sub{$$networks{$a}{name} cmp $$networks{$b}{name}};
for my $id(sort $sortByName keys %$networks){
if($$networks{$id}{type} eq "WLAN_INFRA"){
my $name = $$networks{$id}{name};
my $wpa = $$networks{$id}{EAP_wpa_preshared_passphrase};
my $wep = $$networks{$id}{wlan_wepkey1};
my $key;
$key = "WPA: $wpa" if not defined $key and defined $wpa;
$key = "WEP: $wep" if not defined $key and defined $wep;
$key = "NONE" if not defined $key;
print "$name => $key\n";
}
}
}else{
print "clearing all wifi networks\n";
clearAllWifi();
print "\n\n";
print "adding wifi networks from net\n";
addWifiFromNet();
}
}
sub addWifiFromNet(){
my @cmds;
my %noAuto = map {chomp; $_ => 1} `cat $noAutoFile 2>/dev/null`;
for my $ssidName(sort split /\n/, runBT "winfo", "--list-all"){
my $info = runBT "winfo", $ssidName;
my $ssid = $1 if $info =~ /^ssid:(.*)$/m;
my $enc = $1 if $info =~ /^enc:(.*)$/m;
my $key = $1 if $info =~ /^key:(.*)$/m;
my $mode = $1 if $info =~ /^mode:(.*)$/m;
my $auto = $1 if $info =~ /^auto:(.*)$/m;
my $autoConnect = $auto =~ /^\d+$/ ? 'true' : 'false';
if($enc =~ /^(WPA|WEP|NONE)/i and $mode =~ /managed/i){
if(defined $noAuto{$ssid}){
print " forcibly disabling autoconnect for $ssid\n";
$autoConnect = 'false';
}
my $wifiCmds = getGconfCmds(getWifiGconf $ssid, $enc, $key, $autoConnect);
my $msg = "adding:";
$msg .= " SSID='$ssid'";
$msg .= " NONE" if $enc =~ /^(NONE)$/i;
$msg .= " WEP='$key'" if $enc =~ /^(WEP)$/i;
$msg .= " WPA='$key'" if $enc =~ /^(WPA)$/i;
$msg .= " auto='$autoConnect'";
push @cmds, ["echo", $msg];
@cmds = (@cmds, @$wifiCmds);
}
}
runCmdN9 @cmds;
}
sub clearAllWifi(){
my $networks = getExisting();
my @cmds;
for my $id(keys %$networks){
if($$networks{$id}{type} eq "WLAN_INFRA"){
my $name = $$networks{$id}{name};
$name = '' if not defined $name;
push @cmds, ["echo", "deleting $id => $name"];
push @cmds, ["gconftool", "--recursive-unset", "$rootDir/$id"];
}
}
runCmdN9 @cmds;
}
sub getExisting(){
my @iapLines = `n9 -u user -s gconftool -R $rootDir`;
my %allAtts = (%$wifiAtts, %$gprsAtts);
my $id;
my $networks = {};
my $h4Re = "[a-f0-9]{4}";
my $h8Re = "[a-f0-9]{8}";
my $h12Re = "[a-f0-9]{12}";
my $attRe = join "|", keys %allAtts;
for my $line(@iapLines){
if($line =~ /^ $rootDir\/($h8Re-$h4Re-$h4Re-$h4Re-$h12Re):$/){
$id = $1;
$$networks{$id} = {};
}elsif(defined $id and $line =~ /^ ($attRe) = (.*)$/){
$$networks{$id}{$1} = $2;
}
}
for my $id(keys %$networks){
delete $$networks{$id} if not defined $$networks{$id}{name};
}
return $networks;
}
sub getWifiGconf($$$$){
my ($ssid, $enc, $key, $auto) = @_;
my $id = randomId;
my $dir = "$rootDir/$id";
my $gconf = {};
for my $att(keys %$wifiAtts){
$$gconf{"$dir/$att"} = $$wifiAtts{$att} if defined $$wifiAtts{$att};
}
$$gconf{"$dir/name"} = [$ssid];
$$gconf{"$dir/wlan_ssid"} = ssidListInt $ssid;
if($enc =~ /WPA/i){
$$gconf{"$dir/wlan_security"} = ["WPA_PSK"];
$$gconf{"$dir/EAP_wpa_preshared_key"} = wpaPskListInt $ssid, $key;
$$gconf{"$dir/EAP_wpa_preshared_passphrase"} = [$key];
}elsif($enc =~ /WEP/i){
$$gconf{"$dir/wlan_security"} = ["WEP"];
$$gconf{"$dir/wlan_wepdefkey"} = [1];
$$gconf{"$dir/wlan_wepkey1"} = [$key];
$$gconf{"$dir/EAP_wpa_preshared_passphrase"} = [$key];
}
$$gconf{"$dir/autoconnect"} = $auto =~ /true/i ? $TRUE : $FALSE;
return $gconf;
}
sub getGconfCmds($){
my $gconf = shift;
my $cmds = [];
for my $key(keys %$gconf){
my @arr = @{$$gconf{$key}};
my $val = shift @arr;
my $type = @arr > 0 ? shift @arr : 'string';
my $listType = @arr > 0 ? shift @arr : undef;
push @$cmds, gconfSetCmd($key, $val, $type, $listType);
}
return $cmds;
}
sub gconfGetCmd($){
my $key = shift;
$key =~ s/"/\\"/g;
return ["gconftool-2", "--get", $key];
}
sub gconfSetCmd($$$;$){
my ($key, $val, $type, $listType) = @_;
$listType =~ s/"/\\"/g if defined $listType;
my $cmd = ["gconftool-2", "--set", $key, $val, "--type=$type"];
if(lc $type eq 'list'){
push @$cmd, "--list-type=$listType";
}
return $cmd;
}
sub listInt($){
my @list = @{shift()};
my $val = '[' . (join ',', @list) . ']';
return [$val, 'list', 'int'];
}
sub listString($){
my @list = @{shift()};
my $val = '[' . (join ',', @list) . ']';
return [$val, 'list', 'string'];
}
sub ssidListInt($){
my ($ssid) = @_;
my @dec;
for(my $i=0; $i<length $ssid; $i++){
push @dec, ord(substr $ssid, $i, 1);
}
return listInt(\@dec);
}
sub wpaPskListInt($$){
my ($ssid, $passphrase) = @_;
my $net = runBT "wpa_passphrase", $ssid, $passphrase;
if($net =~ /^\s*psk=([0-9a-f]+)\s*$/m){
my $psk = $1;
my @bytes;
for(my $i=0; $i<length $psk; $i+=2){
push @bytes, substr $psk, $i, 2;
}
my @dec = map {sprintf("%d", hex($_))} @bytes;
return listInt(\@dec);
}
return undef;
}
sub randHex($){
my $digs = shift;
return lc sprintf "%0${digs}X", rand(16**$digs);
}
sub randomId(){
return join '-', randHex(8), randHex(4), randHex(4), randHex(4), randHex(12);
}
sub runCmdN9(@){
my $cmdStr = '';
for my $cmd(@_){
for my $word(@$cmd){
$word =~ s/"/\""/g;
$word = "\"$word\"" if $word =~ /["'` \t\n!><&\$\\]/;
$cmdStr .= " $word";
}
$cmdStr .= "\n";
}
system "n9", "-s", $cmdStr if $cmdStr !~ /^\s*$/;
}
sub runBT(@){
open FH, "-|", @_;
my @lines = <FH>;
close FH;
return join '', @lines;
}
&main(@ARGV);