]>
git.ipfire.org Git - 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'}' />
261 <table width="100%" class='tbl'>
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>
273 # Print each line of @configs list
278 foreach my $line (@configs) {
279 # Skip commented lines.
280 my $firstchar = substr($line, 0, 1);
281 next if ($firstchar eq "#");
284 my @config = split(/\,/,$line);
286 #Choose icon for checkbox
289 if ($config[2] eq 'on' ) {
291 $gdesc = $Lang::tr
{'click to disable'};
294 $gdesc = $Lang::tr
{'click to enable'};
300 $col="bgcolor='$color{'color20'}'";
303 $col="bgcolor='$color{'color22'}'";
306 my $encryption_mode = $Lang::tr
{'unknown'};
307 if ($config[3] eq "NONE") {
308 $encryption_mode = $Lang::tr
{'wlan client encryption none'};
309 } elsif ($config[3] eq "WEP") {
310 $encryption_mode = $Lang::tr
{'wlan client encryption wep'};
311 } elsif ($config[3] eq "WPA") {
312 $encryption_mode = $Lang::tr
{'wlan client encryption wpa'};
313 } elsif ($config[3] eq "WPA2") {
314 $encryption_mode = $Lang::tr
{'wlan client encryption wpa2'};
317 if (($config[3] eq "WPA") || ($config[3] eq "WPA2")) {
318 my $wpa_pairwise = "$Lang::tr{'wlan client ccmp'} $Lang::tr{'wlan client and'} $Lang::tr{'wlan client tkip'}";
319 my $wpa_group = "$Lang::tr{'wlan client ccmp'} $Lang::tr{'wlan client and'} $Lang::tr{'wlan client tkip'}";
321 if ($config[4] eq "CCMP-CCMP") {
322 $wpa_pairwise = $Lang::tr
{'wlan client ccmp'};
323 $wpa_group = $Lang::tr
{'wlan client ccmp'};
324 } elsif ($config[4] eq "CCMP-TKIP") {
325 $wpa_pairwise = $Lang::tr
{'wlan client ccmp'};
326 $wpa_group = $Lang::tr
{'wlan client tkip'};
327 } elsif ($config[4] eq "TKIP-TKIP") {
328 $wpa_pairwise = $Lang::tr
{'wlan client tkip'};
329 $wpa_group = $Lang::tr
{'wlan client tkip'};
332 $encryption_mode .= "<hr>";
333 $encryption_mode .= "<strong>$Lang::tr{'wlan client pairwise key algorithm'}</strong>: ";
334 $encryption_mode .= $wpa_pairwise;
335 $encryption_mode .= "<br>";
336 $encryption_mode .= "<strong>$Lang::tr{'wlan client group key algorithm'}</strong>: ";
337 $encryption_mode .= $wpa_group;
341 <td align='center' $col>$config[5]</td>
342 <td align='center' $col>$encryption_mode</td>
343 <td align='center' $col>$config[7]</td>
344 <td align='center' width='5%' $col>
345 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
346 <input type='hidden' name='ACTION' value='$Lang::tr{'toggle enable disable'}' />
347 <input type='image' name='$Lang::tr{'toggle enable disable'}' src='/images/$gif' alt='$gdesc' title='$gdesc' />
348 <input type='hidden' name='ID' value='$config[0]' />
351 <td align='center' width='5%' $col>
352 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
353 <input type='hidden' name='ACTION' value='$Lang::tr{'edit'}' />
354 <input type='image' name='$Lang::tr{'edit'}' src='/images/edit.gif' alt='$Lang::tr{'edit'}' title='$Lang::tr{'edit'}' />
355 <input type='hidden' name='ID' value='$config[0]' />
358 <td align='center' width='5%' $col>
359 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
360 <input type='hidden' name='ACTION' value='$Lang::tr{'remove'}' />
361 <input type='image' name='$Lang::tr{'remove'}' src='/images/delete.gif' alt='$Lang::tr{'remove'}' title='$Lang::tr{'remove'}' />
362 <input type='hidden' name='ID' value='$config[0]' />
371 # If table contains entries, print 'Key to action icons'
376 <td class='boldbase'> <b>$Lang::tr{'legend'}: </b></td>
377 <td><img src='/images/on.gif' alt='$Lang::tr{'click to disable'}' /></td>
378 <td class='base'>$Lang::tr{'click to disable'}</td>
379 <td> </td>
380 <td><img src='/images/off.gif' alt='$Lang::tr{'click to enable'}' /></td>
381 <td class='base'>$Lang::tr{'click to enable'}</td>
382 <td> </td>
383 <td><img src='/images/edit.gif' alt='$Lang::tr{'edit'}' /></td>
384 <td class='base'>$Lang::tr{'edit'}</td>
385 <td> </td>
386 <td><img src='/images/delete.gif' alt='$Lang::tr{'remove'}' /></td>
387 <td class='base'>$Lang::tr{'remove'}</td>
398 &Header
::closebigbox
();
399 &Header
::closepage
();
403 &Header
::openpage
($Lang::tr
{'wlan client configuration'}, 1, '');
404 &Header
::openbigbox
('100%', 'left', '', $errormessage);
407 &Header
::openbox
('100%', 'left', $Lang::tr
{'error messages'});
408 print "<font class='base'>$errormessage </font>";
412 my $buttontext = $Lang::tr
{'add'};
413 if ($settings{'ID'} ne '') {
414 $buttontext = $Lang::tr
{'update'};
415 &Header
::openbox
('100%', 'left', $Lang::tr
{'wlan client edit entry'});
417 &Header
::openbox
('100%', 'left', $Lang::tr
{'wlan client new entry'});
418 $settings{'ENABLED'} = 'on';
420 my $action = $buttontext;
423 $checked{'ENABLED'} = ($settings{'ENABLED'} ne 'on' ) ?
'' : "checked='checked'";
426 $selected{'ENCRYPTION'} = ();
427 $selected{'ENCRYPTION'}{'NONE'} = '';
428 $selected{'ENCRYPTION'}{'WPA2'} = '';
429 $selected{'ENCRYPTION'}{'WPA'} = '';
430 $selected{'ENCRYPTION'}{'WEP'} = '';
431 $selected{'ENCRYPTION'}{$settings{'ENCRYPTION'}} = "selected='selected'";
433 $selected{'WPA_MODE'} = ();
434 $selected{'WPA_MODE'}{''} = '';
435 $selected{'WPA_MODE'}{'CCMP-CCMP'} = '';
436 $selected{'WPA_MODE'}{'CCMP-TKIP'} = '';
437 $selected{'WPA_MODE'}{'TKIP-TKIP'} = '';
438 $selected{'WPA_MODE'}{$settings{'WPA_MODE'}} = "selected='selected'";
440 $selected{'PRIO'} = ();
441 $selected{'PRIO'}{'0'} = '';
442 $selected{'PRIO'}{'1'} = '';
443 $selected{'PRIO'}{'2'} = '';
444 $selected{'PRIO'}{'3'} = '';
445 $selected{'PRIO'}{'4'} = '';
446 $selected{'PRIO'}{$settings{'PRIO'}} = "selected='selected'";
449 <form method='POST' action='$ENV{'SCRIPT_NAME'}'>
450 <input type='hidden' name='ID' value='$settings{'ID'}'>
454 <td class='base' width='20%'>$Lang::tr{'wlan client ssid'}:</td>
455 <td width='40%'><input type='text' name='SSID' value="$settings{'SSID'}" size='25'/></td>
456 <td class='base' width='10%'>$Lang::tr{'enabled'}</td>
457 <td width='30%'><input type='checkbox' name='ENABLED' $checked{'ENABLED'} /></td>
460 <td class='base' width='20%'>$Lang::tr{'wlan client encryption'}:</td>
462 <select name='ENCRYPTION'>
463 <option value="NONE" $selected{'ENCRYPTION'}{'NONE'}>$Lang::tr{'wlan client encryption none'}</option>
464 <option value="WPA2" $selected{'ENCRYPTION'}{'WPA2'}>$Lang::tr{'wlan client encryption wpa2'}</option>
465 <option value="WPA" $selected{'ENCRYPTION'}{'WPA'}>$Lang::tr{'wlan client encryption wpa'}</option>
466 <option value="WEP" $selected{'ENCRYPTION'}{'WEP'}>$Lang::tr{'wlan client encryption wep'}</option>
469 <td colspan="2" width='40%'></td>
472 <td class='base' width='20%'>$Lang::tr{'wlan client psk'}: </td>
473 <td width='40%'><input type='password' name='PSK' value="$settings{'PSK'}" size='25'/></td>
474 <td colspan="2" width='40%'></td>
482 $Lang::tr{'wlan client advanced settings'}:
487 <td class='base' width='20%'>
488 $Lang::tr{'wlan client wpa mode'}:
491 <select name='WPA_MODE'>
492 <option value="" $selected{'WPA_MODE'}{''}>$Lang::tr{'wlan client wpa mode all'}</option>
493 <option value="CCMP-CCMP" $selected{'WPA_MODE'}{'CCMP-CCMP'}>$Lang::tr{'wlan client wpa mode ccmp ccmp'}</option>
494 <option value="CCMP-TKIP" $selected{'WPA_MODE'}{'CCMP-TKIP'}>$Lang::tr{'wlan client wpa mode ccmp tkip'}</option>
495 <option value="TKIP-TKIP" $selected{'WPA_MODE'}{'TKIP-TKIP'}>$Lang::tr{'wlan client wpa mode tkip tkip'}</option>
498 <td colspan="2" width='40%'>
499 <em>($Lang::tr{'wlan client pairwise key group key'})</em>
503 <td class='base' width='20%'>
504 $Lang::tr{'priority'}:
508 <option value="0" $selected{'PRIO'}{'0'}>0 ($Lang::tr{'most preferred'})</option>
509 <option value="1" $selected{'PRIO'}{'1'}>1</option>
510 <option value="2" $selected{'PRIO'}{'2'}>2</option>
511 <option value="3" $selected{'PRIO'}{'3'}>3</option>
512 <option value="4" $selected{'PRIO'}{'4'}>4 ($Lang::tr{'least preferred'})</option>
515 <td colspan="2" width='40%'></td>
524 <td width='50%' align='center'>
525 <input type='hidden' name='ACTION' value='$action' />
526 <input type='submit' name='SUBMIT' value='$buttontext' />
534 &Header
::closebigbox
();
535 &Header
::closepage
();
539 my $device = $netsettings{'RED_DEV'};
541 # Exit if no device is configured.
542 return if ($device eq "");
544 # Exit if wpa_supplicant is not running on this interface.
545 #return if (! -e "/var/run/wpa_supplicant/$device");
547 open(FILE
, "/usr/local/bin/wirelessclient status |");
553 my ($key, $value) = split("=", $_);
554 $status{$key} = $value;
559 # End here, if no there is no input.
560 return if (!keys %status);
562 &Header
::openbox
('100%', 'left', $Lang::tr
{'status'});
564 if ($status{'ssid'} eq "") {
565 print "<p>$Lang::tr{'wlan client disconnected'}</p>";
572 $Lang::tr{'wlan client ssid'}
580 $Lang::tr{'wlan client bssid'}
588 if (($status{'pairwise_cipher'} ne "NONE") || ($status{'group_cipher'} ne "NONE")) {
592 <strong>$Lang::tr{'wlan client encryption wpa'}</strong>
597 $Lang::tr{'wlan client pairwise cipher'}
600 $status{'pairwise_cipher'}
605 $Lang::tr{'wlan client group cipher'}
608 $status{'group_cipher'}
620 sub BuildConfiguration
() {
621 system("/usr/local/bin/wirelessclient restart");
626 foreach my $line (@configs) {
627 # Skip commented lines.
628 my $firstchar = substr($line, 0, 1);
629 next if ($firstchar eq "#");
631 my @config = split(/\,/, $line);
632 if ($config[0] > $highest_id) {
633 $highest_id = $config[0];
637 return $highest_id + 1;
640 sub DuplicateSSID
($) {
643 foreach my $line (@configs) {
644 # Skip commented lines.
645 my $firstchar = substr($line, 0, 1);
646 next if ($firstchar eq "#");
648 my @config = split(/\,/, $line);
649 if ($config[5] eq $ssid) {
657 sub ValidKeyLength
($$) {
661 my $key_length = length($key);
663 if ($algo eq "WEP") {
664 # Key must be 13 or 26 characters.
665 if (($key_length == 13) || ($key_length == 26)) {
671 } elsif (($algo eq "WPA2") || ($algo eq "WPA")) {
672 # Key must be between 8 and 63 chars.
673 if (($key_length >= 8) && ($key_length <= 63)) {
680 # Say okay for all other algorithms.
684 sub ValidateInput
($) {
687 # Check for duplicate SSIDs.
688 if (($mode eq "add") && (DuplicateSSID
($settings{'SSID'}))) {
689 return "$Lang::tr{'wlan client duplicate ssid'}: $settings{'SSID'}";
691 # Check for invalid key length.
692 } elsif (ValidKeyLength
($settings{'ENCRYPTION'}, $settings{'PSK'})) {
693 return "$Lang::tr{'wlan client invalid key length'}";
697 # Reset WPA mode, if WPA(2) is not selected.
698 if (($settings{'ENCRYPTION'} ne "WPA") && ($settings{'ENCRYPTION'} ne "WPA2")) {
699 $settings{'WPA_MODE'} = '';
702 if ($settings{'ENABLED'} ne "") {
703 $settings{'ENABLED'} = 'on';
705 $settings{'ENABLED'} = 'off';