]>
git.ipfire.org Git - people/mfischer/ipfire-2.x.git/blob - html/cgi-bin/wirelessclient.cgi
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2012 IPFire Team <info@ipfire.org> #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
22 # DEVICE,ENABLED,MODE,WPA_MODE,SSID,PSK,PRIO
23 # wlan0,on,WPA2,,Use This One Mum,ThisIsTheKey,2
27 # enable only the following on debugging purpose
29 use CGI
::Carp
'fatalsToBrowser';
31 require '/var/ipfire/general-functions.pl';
32 require "${General::swroot}/lang.pl";
33 require "${General::swroot}/header.pl";
35 #workaround to suppress a warning when a variable is used only once
36 my @dummy = ( ${Header
::colouryellow
} );
40 my $setting = "${General::swroot}/main/settings";
41 our $datafile = "${General::swroot}/ethernet/wireless";
44 my %mainsettings = ();
45 &General
::readhash
("${General::swroot}/main/settings", \
%mainsettings);
46 &General
::readhash
("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \
%color);
49 our %netsettings = ();
52 $settings{'INTERFACE'} = '';
53 $settings{'ENABLED'} = '';
54 $settings{'ENCRYPTION'} = '';
55 $settings{'WPA_MODE'} = '';
56 $settings{'SSID'} = '';
57 $settings{'PSK'} = '';
58 $settings{'PRIO'} = '';
60 $settings{'ACTION'} = ''; # add/edit/remove
61 $settings{'ID'} = ''; # point record for ACTION
63 my $errormessage = '';
66 &Header
::showhttpheaders
();
69 &Header
::getcgihash
(\
%settings);
73 if (open(FILE
, "$datafile")) {
78 &General
::readhash
("${General::swroot}/main/settings", \
%settings);
79 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
81 # Toggle enable/disable field.
82 if ($settings{'ACTION'} eq $Lang::tr
{'toggle enable disable'}) {
85 foreach my $line (@configs) {
87 my @config = split(/\,/, $line);
89 # Update the entry with the matching ID.
90 if ($config[0] eq $settings{'ID'}) {
91 # Toggle enabled/disabled status.
93 if ($config[2] eq 'on') {
99 $line = join(',', @config);
102 push(@update, $line."\n");
105 # Save updated configuration settings.
106 open(FILE
, ">$datafile") or die 'wlan client datafile error';
112 # Update configuration files.
113 &BuildConfiguration
();
116 $settings{'ACTION'} = '';
119 if ($settings{'ACTION'} eq $Lang::tr
{'add'}) {
120 # Validate input data.
121 $errormessage = ValidateInput
("add");
123 unless ($errormessage) {
124 # Search for the next free id.
125 my $next_id = NextID
();
127 my @config = ($next_id);
128 push(@config, $settings{'INTERFACE'});
129 push(@config, $settings{'ENABLED'});
130 push(@config, $settings{'ENCRYPTION'});
131 push(@config, $settings{'WPA_MODE'});
132 push(@config, $settings{'SSID'});
133 push(@config, $settings{'PSK'});
134 push(@config, $settings{'PRIO'});
136 # Add the new configuration and write all the stuff to the configuration file.
137 my $line = join(',', @config) . "\n";
138 push(@configs, $line);
140 # Save updated configuration settings.
141 open(FILE
, ">$datafile") or die 'wlan client datafile error';
145 # Update configuration files.
146 &BuildConfiguration
();
149 $settings{'ACTION'} = '';
153 if ($settings{'ACTION'} eq $Lang::tr
{'edit'}) {
154 foreach my $line (@configs) {
156 my @config = split(/\,/, $line);
158 if ($config[0] eq $settings{'ID'}) {
159 $settings{'ID'} = $config[0];
160 $settings{'INTERFACE'} = $config[1];
161 $settings{'ENABLED'} = $config[2];
162 $settings{'ENCRYPTION'} = $config[3];
163 $settings{'WPA_MODE'} = $config[4];
164 $settings{'SSID'} = $config[5];
165 $settings{'PSK'} = $config[6];
166 $settings{'PRIO'} = $config[7];
171 if ($settings{'ACTION'} eq $Lang::tr
{'update'}) {
172 $errormessage = ValidateInput
("update");
174 unless ($errormessage) {
176 foreach my $line (@configs) {
178 my @config = split(/\,/, $line);
180 # Update the entry with the matching ID.
181 if ($config[0] eq $settings{'ID'}) {
182 # Update all configuration settings.
183 # ID and INTERFACE cannot be changed.
184 $config[2] = $settings{'ENABLED'};
185 $config[3] = $settings{'ENCRYPTION'};
186 $config[4] = $settings{'WPA_MODE'};
187 $config[5] = $settings{'SSID'};
188 $config[6] = $settings{'PSK'};
189 $config[7] = $settings{'PRIO'};
191 $line = join(',', @config);
194 push(@update, $line."\n");
197 # Save updated configuration settings.
198 open(FILE
, ">$datafile") or die 'wlan client datafile error';
204 # Update configuration files.
205 &BuildConfiguration
();
208 $settings{'ACTION'} = '';
212 if ($settings{'ACTION'} eq $Lang::tr
{'remove'}) {
215 foreach my $line (@configs) {
217 my @config = split(/\,/, $line);
219 # Skip the to be removed entry.
220 if ($config[0] eq $settings{'ID'}) {
224 push(@update, $line."\n");
227 # Save updated configuration settings.
228 open(FILE
, ">$datafile") or die 'wlan client datafile error';
234 # Update configuration files.
235 &BuildConfiguration
();
238 $settings{'ACTION'} = '';
241 if ($settings{'ACTION'} eq '') { # First launch from GUI
244 # Action has been set, so show the edit box.
249 &Header
::openpage
($Lang::tr
{'wlan client configuration'}, 1, '');
250 &Header
::openbigbox
('100%', 'left', '', $errormessage);
252 &Header
::openbox
('100%', 'left', $Lang::tr
{'wlan client configuration'});
255 <form method='POST' action='$ENV{'SCRIPT_NAME'}' style='text-align: center;'>
256 <input type='submit' name='ACTION' value='$Lang::tr{'wlan client new network'}' />
263 <th align='center'>$Lang::tr{'wlan client ssid'}</th>
264 <th align='center'>$Lang::tr{'wlan client encryption'}</th>
265 <th align='center'>$Lang::tr{'priority'}</th>
270 # Print each line of @configs list
274 foreach my $line (@configs) {
275 # Skip commented lines.
276 my $firstchar = substr($line, 0, 1);
277 next if ($firstchar eq "#");
280 my @config = split(/\,/,$line);
282 #Choose icon for checkbox
285 if ($config[2] eq 'on' ) {
287 $gdesc = $Lang::tr
{'click to disable'};
290 $gdesc = $Lang::tr
{'click to enable'};
295 print "<tr bgcolor='$color{'color22'}'>";
297 print "<tr bgcolor='$color{'color20'}'>";
300 my $encryption_mode = $Lang::tr
{'unknown'};
301 if ($config[3] eq "NONE") {
302 $encryption_mode = $Lang::tr
{'wlan client encryption none'};
303 } elsif ($config[3] eq "WEP") {
304 $encryption_mode = $Lang::tr
{'wlan client encryption wep'};
305 } elsif ($config[3] eq "WPA") {
306 $encryption_mode = $Lang::tr
{'wlan client encryption wpa'};
307 } elsif ($config[3] eq "WPA2") {
308 $encryption_mode = $Lang::tr
{'wlan client encryption wpa2'};
311 if (($config[3] eq "WPA") || ($config[3] eq "WPA2")) {
312 my $wpa_pairwise = "$Lang::tr{'wlan client ccmp'} $Lang::tr{'wlan client and'} $Lang::tr{'wlan client tkip'}";
313 my $wpa_group = "$Lang::tr{'wlan client ccmp'} $Lang::tr{'wlan client and'} $Lang::tr{'wlan client tkip'}";
315 if ($config[4] eq "CCMP-CCMP") {
316 $wpa_pairwise = $Lang::tr
{'wlan client ccmp'};
317 $wpa_group = $Lang::tr
{'wlan client ccmp'};
318 } elsif ($config[4] eq "CCMP-TKIP") {
319 $wpa_pairwise = $Lang::tr
{'wlan client ccmp'};
320 $wpa_group = $Lang::tr
{'wlan client tkip'};
321 } elsif ($config[4] eq "TKIP-TKIP") {
322 $wpa_pairwise = $Lang::tr
{'wlan client tkip'};
323 $wpa_group = $Lang::tr
{'wlan client tkip'};
326 $encryption_mode .= "<hr>";
327 $encryption_mode .= "<strong>$Lang::tr{'wlan client pairwise key algorithm'}</strong>: ";
328 $encryption_mode .= $wpa_pairwise;
329 $encryption_mode .= "<br>";
330 $encryption_mode .= "<strong>$Lang::tr{'wlan client group key algorithm'}</strong>: ";
331 $encryption_mode .= $wpa_group;
335 <td align='center'>$config[5]</td>
336 <td align='center'>$encryption_mode</td>
337 <td align='center'>$config[7]</td>
338 <td align='center' width='5%'>
339 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
340 <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
341 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
342 <input type='hidden' name='ID' value='$config[0]' />
345 <td align='center' width='5%'>
346 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
347 <input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
348 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
349 <input type='hidden' name='ID' value='$config[0]' />
352 <td align='center' width='5%'>
353 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
354 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}' />
355 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
356 <input type='hidden' name='ID' value='$config[0]' />
365 # If table contains entries, print 'Key to action icons'
370 <td class='boldbase'> <b>$Lang::tr{'legend'}: </b></td>
371 <td><img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
372 <td class='base'>$Lang::tr{'click to disable'}</td>
373 <td> </td>
374 <td><img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
375 <td class='base'>$Lang::tr{'click to enable'}</td>
376 <td> </td>
377 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
378 <td class='base'>$Lang::tr{'edit'}</td>
379 <td> </td>
380 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
381 <td class='base'>$Lang::tr{'remove'}</td>
392 &Header
::closebigbox
();
393 &Header
::closepage
();
397 &Header
::openpage
($Lang::tr
{'wlan client configuration'}, 1, '');
398 &Header
::openbigbox
('100%', 'left', '', $errormessage);
401 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
402 print "<font class='base'>$errormessage </font>";
406 my $buttontext = $Lang::tr
{'add'};
407 if ($settings{'ID'} ne '') {
408 $buttontext = $Lang::tr
{'update'};
409 &Header
::openbox
('100%', 'left', $Lang::tr
{'wlan client edit entry'});
411 &Header
::openbox
('100%', 'left', $Lang::tr
{'wlan client new entry'});
412 $settings{'ENABLED'} = 'on';
414 my $action = $buttontext;
417 $checked{'ENABLED'} = ($settings{'ENABLED'} ne 'on' ) ?
'' : "checked='checked'";
420 $selected{'ENCRYPTION'} = ();
421 $selected{'ENCRYPTION'}{'NONE'} = '';
422 $selected{'ENCRYPTION'}{'WPA2'} = '';
423 $selected{'ENCRYPTION'}{'WPA'} = '';
424 $selected{'ENCRYPTION'}{'WEP'} = '';
425 $selected{'ENCRYPTION'}{$settings{'ENCRYPTION'}} = "selected='selected'";
427 $selected{'WPA_MODE'} = ();
428 $selected{'WPA_MODE'}{''} = '';
429 $selected{'WPA_MODE'}{'CCMP-CCMP'} = '';
430 $selected{'WPA_MODE'}{'CCMP-TKIP'} = '';
431 $selected{'WPA_MODE'}{'TKIP-TKIP'} = '';
432 $selected{'WPA_MODE'}{$settings{'WPA_MODE'}} = "selected='selected'";
434 $selected{'PRIO'} = ();
435 $selected{'PRIO'}{'0'} = '';
436 $selected{'PRIO'}{'1'} = '';
437 $selected{'PRIO'}{'2'} = '';
438 $selected{'PRIO'}{'3'} = '';
439 $selected{'PRIO'}{'4'} = '';
440 $selected{'PRIO'}{$settings{'PRIO'}} = "selected='selected'";
443 <form method='POST' action='$ENV{'SCRIPT_NAME'}'>
444 <input type='hidden' name='ID' value='$settings{'ID'}'>
448 <td class='base' width='20%'>$Lang::tr{'wlan client ssid'}:</td>
449 <td width='40%'><input type='text' name='SSID' value='$settings{'SSID'}' size='25'/></td>
450 <td class='base' width='10%'>$Lang::tr{'enabled'}</td>
451 <td width='30%'><input type='checkbox' name='ENABLED' $checked{'ENABLED'} /></td>
454 <td class='base' width='20%'>$Lang::tr{'wlan client encryption'}:</td>
456 <select name='ENCRYPTION'>
457 <option value="NONE" $selected{'ENCRYPTION'}{'NONE'}>$Lang::tr{'wlan client encryption none'}</option>
458 <option value="WPA2" $selected{'ENCRYPTION'}{'WPA2'}>$Lang::tr{'wlan client encryption wpa2'}</option>
459 <option value="WPA" $selected{'ENCRYPTION'}{'WPA'}>$Lang::tr{'wlan client encryption wpa'}</option>
460 <option value="WEP" $selected{'ENCRYPTION'}{'WEP'}>$Lang::tr{'wlan client encryption wep'}</option>
463 <td colspan="2" width='40%'></td>
466 <td class='base' width='20%'>$Lang::tr{'wlan client psk'}: </td>
467 <td width='40%'><input type='password' name='PSK' value='$settings{'PSK'}' size='25'/></td>
468 <td colspan="2" width='40%'></td>
476 $Lang::tr{'wlan client advanced settings'}:
481 <td class='base' width='20%'>
482 $Lang::tr{'wlan client wpa mode'}:
485 <select name='WPA_MODE'>
486 <option value="" $selected{'WPA_MODE'}{''}>$Lang::tr{'wlan client wpa mode all'}</option>
487 <option value="CCMP-CCMP" $selected{'WPA_MODE'}{'CCMP-CCMP'}>$Lang::tr{'wlan client wpa mode ccmp ccmp'}</option>
488 <option value="CCMP-TKIP" $selected{'WPA_MODE'}{'CCMP-TKIP'}>$Lang::tr{'wlan client wpa mode ccmp tkip'}</option>
489 <option value="TKIP-TKIP" $selected{'WPA_MODE'}{'TKIP-TKIP'}>$Lang::tr{'wlan client wpa mode tkip tkip'}</option>
492 <td colspan="2" width='40%'>
493 <em>($Lang::tr{'wlan client pairwise key group key'})</em>
497 <td class='base' width='20%'>
498 $Lang::tr{'priority'}:
502 <option value="0" $selected{'PRIO'}{'0'}>0 ($Lang::tr{'most preferred'})</option>
503 <option value="1" $selected{'PRIO'}{'1'}>1</option>
504 <option value="2" $selected{'PRIO'}{'2'}>2</option>
505 <option value="3" $selected{'PRIO'}{'3'}>3</option>
506 <option value="4" $selected{'PRIO'}{'4'}>4 ($Lang::tr{'least preferred'})</option>
509 <td colspan="2" width='40%'></td>
518 <td width='50%' align='center'>
519 <input type='hidden' name='ACTION' value='$action' />
520 <input type='submit' name='SUBMIT' value='$buttontext' />
528 &Header
::closebigbox
();
529 &Header
::closepage
();
533 my $device = $netsettings{'RED_DEV'};
535 # Exit if no device is configured.
536 return if ($device eq "");
538 # Exit if wpa_supplicant is not running on this interface.
539 #return if (! -e "/var/run/wpa_supplicant/$device");
541 open(FILE
, "/usr/local/bin/wirelessclient status |");
547 my ($key, $value) = split("=", $_);
548 $status{$key} = $value;
553 # End here, if no there is no input.
554 return if (!keys %status);
556 &Header
::openbox
('100%', 'left', $Lang::tr
{'status'});
558 if ($status{'ssid'} eq "") {
559 print "<p>$Lang::tr{'wlan client disconnected'}</p>";
566 $Lang::tr{'wlan client ssid'}
574 $Lang::tr{'wlan client bssid'}
582 if (($status{'pairwise_cipher'} ne "NONE") || ($status{'group_cipher'} ne "NONE")) {
586 <strong>$Lang::tr{'wlan client encryption wpa'}</strong>
591 $Lang::tr{'wlan client pairwise cipher'}
594 $status{'pairwise_cipher'}
599 $Lang::tr{'wlan client group cipher'}
602 $status{'group_cipher'}
614 sub BuildConfiguration
() {
615 system("/usr/local/bin/wirelessclient restart");
620 foreach my $line (@configs) {
621 # Skip commented lines.
622 my $firstchar = substr($line, 0, 1);
623 next if ($firstchar eq "#");
625 my @config = split(/\,/, $line);
626 if ($config[0] > $highest_id) {
627 $highest_id = $config[0];
631 return $highest_id + 1;
634 sub DuplicateSSID
($) {
637 foreach my $line (@configs) {
638 # Skip commented lines.
639 my $firstchar = substr($line, 0, 1);
640 next if ($firstchar eq "#");
642 my @config = split(/\,/, $line);
643 if ($config[5] eq $ssid) {
651 sub ValidKeyLength
($$) {
655 my $key_length = length($key);
657 if ($algo eq "WEP") {
658 # Key must be 13 or 26 characters.
659 if (($key_length == 13) || ($key_length == 26)) {
665 } elsif (($algo eq "WPA2") || ($algo eq "WPA")) {
666 # Key must be between 8 and 63 chars.
667 if (($key_length >= 8) && ($key_length <= 63)) {
674 # Say okay for all other algorithms.
678 sub ValidateInput
($) {
681 # Check for duplicate SSIDs.
682 if (($mode eq "add") && (DuplicateSSID
($settings{'SSID'}))) {
683 return "$Lang::tr{'wlan client duplicate ssid'}: $settings{'SSID'}";
685 # Check for invalid key length.
686 } elsif (ValidKeyLength
($settings{'ENCRYPTION'}, $settings{'PSK'})) {
687 return "$Lang::tr{'wlan client invalid key length'}";
691 # Reset WPA mode, if WPA(2) is not selected.
692 if (($settings{'ENCRYPTION'} ne "WPA") && ($settings{'ENCRYPTION'} ne "WPA2")) {
693 $settings{'WPA_MODE'} = '';
696 if ($settings{'ENABLED'} ne "") {
697 $settings{'ENABLED'} = 'on';
699 $settings{'ENABLED'} = 'off';