]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/perl | |
2 | ############################################################################### | |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
5 | # Copyright (C) 2009 Michael Tremer & Christian Schmidt # | |
6 | # # | |
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. # | |
11 | # # | |
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. # | |
16 | # # | |
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/>. # | |
19 | # # | |
20 | ############################################################################### | |
21 | # | |
22 | # WLAN AP cgi based on wlanap.cgi written by Markus Hoffmann & Olaf Westrik | |
23 | # | |
24 | ||
25 | use strict; | |
26 | ||
27 | # enable only the following on debugging purpose | |
28 | #use warnings; | |
29 | #use CGI::Carp 'fatalsToBrowser'; | |
30 | ||
31 | require '/var/ipfire/general-functions.pl'; | |
32 | require '/var/ipfire/lang.pl'; | |
33 | require '/var/ipfire/header.pl'; | |
34 | ||
35 | my $debug = 0; | |
36 | my $status = ''; | |
37 | my $errormessage = ''; | |
38 | my $status_started = "<td align='center' width='75%' bgcolor='${Header::colourgreen}'><font color='white'><b>$Lang::tr{'running'}</b></font></td></tr>"; | |
39 | my $status_stopped = "<td align='center' width='75%' bgcolor='${Header::colourred}'><font color='white'><b>$Lang::tr{'stopped'}</b></font></td></tr>"; | |
40 | ||
41 | # get rid of used only once warnings | |
42 | my @onlyonce = ( $Header::colourgreen, $Header::colourred ); | |
43 | undef @onlyonce; | |
44 | ||
45 | my %selected=(); | |
46 | my %checked=(); | |
47 | my %color = (); | |
48 | my %mainsettings = (); | |
49 | my %netsettings=(); | |
50 | my %wlanapsettings=(); | |
51 | my $channel = ''; | |
52 | my $txpower = ''; | |
53 | ||
54 | &General::readhash("${General::swroot}/main/settings", \%mainsettings); | |
55 | &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color); | |
56 | &General::readhash("/var/ipfire/ethernet/settings", \%netsettings); | |
57 | ||
58 | $wlanapsettings{'APMODE'} = 'on'; | |
59 | $wlanapsettings{'ACTION'} = ''; | |
60 | $wlanapsettings{'MACMODE'} = '0'; | |
61 | $wlanapsettings{'INTERFACE'} = ''; | |
62 | $wlanapsettings{'SSID'} = 'IPFire'; | |
63 | $wlanapsettings{'HIDESSID'} = 'off'; | |
64 | $wlanapsettings{'ENC'} = 'wpa2'; # none / wpa1 /wpa2 | |
65 | $wlanapsettings{'TXPOWER'} = 'auto'; | |
66 | $wlanapsettings{'CHANNEL'} = '05'; | |
67 | $wlanapsettings{'HW_MODE'} = 'g'; | |
68 | $wlanapsettings{'PWD'} = 'IPFire-2.x'; | |
69 | $wlanapsettings{'SYSLOGLEVEL'} = '0'; | |
70 | $wlanapsettings{'DEBUG'} = '4'; | |
71 | $wlanapsettings{'DRIVER'} = 'MADWIFI'; | |
72 | ||
73 | &General::readhash("/var/ipfire/wlanap/settings", \%wlanapsettings); | |
74 | &Header::getcgihash(\%wlanapsettings); | |
75 | ||
76 | my @macs = $wlanapsettings{'MACS'}; | |
77 | ||
78 | delete $wlanapsettings{'__CGI__'}; | |
79 | delete $wlanapsettings{'x'}; | |
80 | delete $wlanapsettings{'y'}; | |
81 | delete $wlanapsettings{'MACS'}; | |
82 | delete $wlanapsettings{'ACCEPT_MACS'}; | |
83 | delete $wlanapsettings{'DENY_MACS'}; | |
84 | ||
85 | &Header::showhttpheaders(); | |
86 | ||
87 | if ( $wlanapsettings{'ACTION'} eq "$Lang::tr{'save'}" ){ | |
88 | # verify WPA Passphrase, must be 8 .. 63 characters | |
89 | if ( (length($wlanapsettings{'PWD'}) < 8) || (length($wlanapsettings{'PWD'}) > 63) ){ | |
90 | $errormessage .= "Invalid length in WPA Passphrase. Must be between 8 and 63 characters.<br />"; | |
91 | } | |
92 | ||
93 | if ( $errormessage eq '' ){ | |
94 | &General::writehash("/var/ipfire/wlanap/settings", \%wlanapsettings); | |
95 | &WriteConfig_hostapd(); | |
96 | ||
97 | system("/usr/local/bin/wlanapctrl restart >/dev/null 2>&1"); | |
98 | } | |
99 | }elsif ( $wlanapsettings{'ACTION'} eq "$Lang::tr{'interface'}" ){ | |
100 | &General::writehash("/var/ipfire/wlanap/settings", \%wlanapsettings); | |
101 | }elsif ( $wlanapsettings{'ACTION'} eq 'Start' ){ | |
102 | system("/usr/local/bin/wlanapctrl start >/dev/null 2>&1"); | |
103 | }elsif ( $wlanapsettings{'ACTION'} eq 'Stop' ){ | |
104 | system("/usr/local/bin/wlanapctrl stop >/dev/null 2>&1"); | |
105 | }elsif ( $wlanapsettings{'ACTION'} eq 'Restart' ){ | |
106 | system("/usr/local/bin/wlanapctrl restart >/dev/null 2>&1"); | |
107 | } | |
108 | ||
109 | &Header::openpage('WLAN', 1, '', ''); | |
110 | &Header::openbigbox('100%', 'left', '', $errormessage); | |
111 | ||
112 | if ( $errormessage ){ | |
113 | &Header::openbox('100%', 'center', $Lang::tr{'error messages'}); | |
114 | print "<class name='base'>$errormessage\n"; | |
115 | print " </class>\n"; | |
116 | &Header::closebox(); | |
117 | } | |
118 | ||
119 | ||
120 | # Found this usefull piece of code in BlockOutTraffic AddOn 8-) | |
121 | # fwrules.cgi | |
122 | ############### | |
123 | # DEBUG DEBUG | |
124 | if ( $debug ){ | |
125 | &Header::openbox('100%', 'center', 'DEBUG'); | |
126 | my $debugCount = 0; | |
127 | foreach my $line (sort keys %wlanapsettings) { | |
128 | print "$line = '$wlanapsettings{$line}'<br />\n"; | |
129 | $debugCount++; | |
130 | } | |
131 | print " Count: $debugCount\n"; | |
132 | &Header::closebox(); | |
133 | } | |
134 | # DEBUG DEBUG | |
135 | ############### | |
136 | ||
137 | # | |
138 | # Driver and status detection | |
139 | # | |
140 | my $wlan_card_status = 'dummy'; | |
141 | my $wlan_ap_status = ''; | |
142 | my $message = ""; | |
143 | ||
144 | $selected{'INTERFACE'}{'green0'} = ''; | |
145 | $selected{'INTERFACE'}{'blue0'} = ''; | |
146 | $selected{'ENC'}{$wlanapsettings{'INTERFACE'}} = "selected='selected'"; | |
147 | ||
148 | if ( ($wlanapsettings{'INTERFACE'} eq '') ){ | |
149 | $message = "No WLan Interface selected."; | |
150 | &Header::openbox('100%', 'center', "WLAN AP"); | |
151 | print <<END | |
152 | $message<br /> | |
153 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
154 | <select name='INTERFACE'> | |
155 | <option value='green0' $selected{'INTERFACE'}{'green0'}>green0</option> | |
156 | END | |
157 | ; | |
158 | if ( $netsettings{'BLUE_DEV'} ne ''){ | |
159 | print "<option value='blue0' $selected{'INTERFACE'}{'blue0'}>blue0</option>"; | |
160 | } | |
161 | print <<END | |
162 | </select> | |
163 | <br /> | |
164 | <input type='hidden' name='ACTION' value='$Lang::tr{'interface'}' /> | |
165 | <input type='image' alt='$Lang::tr{'save'}' title='$Lang::tr{'save'}' src='/images/media-floppy.png' /></form> | |
166 | END | |
167 | ; | |
168 | &Header::closebox(); | |
169 | &Header::closebigbox(); | |
170 | &Header::closepage(); | |
171 | exit; | |
172 | }else{ | |
173 | my $cmd_out = `/usr/sbin/iwconfig $wlanapsettings{'INTERFACE'} 2>/dev/null`; | |
174 | ||
175 | if ( $cmd_out eq '' ){ | |
176 | $message = "Interface is not a WLAN card."; | |
177 | $wlan_card_status = ''; | |
178 | }else{ | |
179 | $cmd_out = `/sbin/ifconfig | /bin/grep $wlanapsettings{'INTERFACE'}`; | |
180 | if ( $cmd_out eq '' ){ | |
181 | $wlan_card_status = 'down'; | |
182 | }else{ | |
183 | $wlan_card_status = 'up'; | |
184 | $cmd_out = `/usr/sbin/iwconfig $wlanapsettings{'INTERFACE'} | /bin/grep "Mode:Master"`; | |
185 | if ( $cmd_out ne '' ){ | |
186 | $wlan_ap_status = 'up'; | |
187 | } | |
188 | } | |
189 | } | |
190 | } | |
191 | ||
192 | my $checked_hidessid = ''; | |
193 | $checked_hidessid = "checked='checked'" if ( $wlanapsettings{'HIDESSID'} eq 'on' ); | |
194 | ||
195 | $selected{'ENC'}{$wlanapsettings{'ENC'}} = "selected='selected'"; | |
196 | $selected{'CHANNEL'}{$wlanapsettings{'CHANNEL'}} = "selected='selected'"; | |
197 | $selected{'TXPOWER'}{$wlanapsettings{'TXPOWER'}} = "selected='selected'"; | |
198 | $selected{'HW_MODE'}{$wlanapsettings{'HW_MODE'}} = "selected='selected'"; | |
199 | $selected{'MACMODE'}{$wlanapsettings{'MACMODE'}} = "selected='selected'"; | |
200 | ||
201 | my @channellist_cmd = `iwlist $wlanapsettings{'INTERFACE'} channel`; | |
202 | # get available channels | |
203 | ||
204 | my @temp; | |
205 | foreach (@channellist_cmd){ | |
206 | $_ =~ /(.*)Channel (\d+)(.*):/; | |
207 | $channel = $2;chomp $channel; | |
208 | if ( $channel =~ /\d+/ ){push(@temp,$channel);} | |
209 | } | |
210 | my @channellist = @temp; | |
211 | ||
212 | my @txpower_cmd = `iwlist $wlanapsettings{'INTERFACE'} txpower`; | |
213 | # get available channels | |
214 | ||
215 | my @temp; | |
216 | foreach (@txpower_cmd){ | |
217 | $_ =~ /(\s)(\d+)(\s)dBm(\s)(.*)(\W)(\d+)(.*)/; | |
218 | $txpower = $7;chomp $txpower; | |
219 | if ( $txpower =~ /\d+/ ){push(@temp,$txpower."mW");} | |
220 | } | |
221 | my @txpower = @temp; | |
222 | push(@txpower,"auto"); | |
223 | ||
224 | $selected{'SYSLOGLEVEL'}{$wlanapsettings{'SYSLOGLEVEL'}} = "selected='selected'"; | |
225 | $selected{'DEBUG'}{$wlanapsettings{'DEBUG'}} = "selected='selected'"; | |
226 | ||
227 | # | |
228 | # Status box | |
229 | # | |
230 | &Header::openbox('100%', 'center', "WLAN AP"); | |
231 | print <<END | |
232 | <table width='95%' cellspacing='0'> | |
233 | <tr><td bgcolor='$color{'color20'}' colspan='2' align='left'><b>WLAN Services</b></td></tr> | |
234 | END | |
235 | ; | |
236 | if ( $wlan_card_status ne '' ){ | |
237 | print "<tr><td class='base'>WLAN card ($wlanapsettings{'DRIVER'})</td>"; | |
238 | print $wlan_card_status eq 'up' ? $status_started : $status_stopped; | |
239 | print "<tr><td class='base'>Access Point</td>"; | |
240 | print $wlan_ap_status eq 'up' ? $status_started : $status_stopped; | |
241 | }else{ | |
242 | print "<tr><td colspan='2' class='base'><b>$message</b></td></tr>"; | |
243 | } | |
244 | print "</table>"; | |
245 | ||
246 | print <<END | |
247 | <br /> | |
248 | <table width='95%' cellspacing='0'> | |
249 | <tr><td align='left' width='40%' /> | |
250 | <td align='center' ><form method='post' action='$ENV{'SCRIPT_NAME'}'><input type='hidden' name='ACTION' value='Start' /><input type='image' alt='Start' title='Start' src='/images/go-up.png' /></form></td> | |
251 | <td align='center' ><form method='post' action='$ENV{'SCRIPT_NAME'}'><input type='hidden' name='ACTION' value='Stop' /><input type='image' alt='Stop' title='Stop' src='/images/go-down.png' /></form></td> | |
252 | <td align='center' ><form method='post' action='$ENV{'SCRIPT_NAME'}'><input type='hidden' name='ACTION' value='Restart' /><input type='image' alt='Restart' title='Restart' src='/images/view-refresh.png' /></form></td></tr> | |
253 | </table> | |
254 | END | |
255 | ; | |
256 | ||
257 | if ( $wlan_card_status eq '' ){ | |
258 | &Header::closebox(); | |
259 | &Header::closebigbox(); | |
260 | &Header::closepage(); | |
261 | exit 0; | |
262 | } | |
263 | print <<END | |
264 | <br /> | |
265 | <form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
266 | <table width='95%' cellspacing='0'> | |
267 | <tr><td bgcolor='$color{'color20'}' colspan='4' align='left'><b>WLAN Settings</b> | |
268 | <tr><td width='25%' class='base'>SSID: </td><td class='base' colspan='3'><input type='text' name='SSID' size='40' value='$wlanapsettings{'SSID'}' /></td></tr> | |
269 | <tr><td width='25%' class='base'>Disable SSID broadcast: </td><td class='base' colspan='3'><input type='checkbox' name='HIDESSID' $checked_hidessid /></td></tr> | |
270 | <tr><td width='25%' class='base'>HW Mode: </td><td class='base' colspan='3'> | |
271 | <select name='HW_MODE'> | |
272 | <option value='a' $selected{'HW_MODE'}{'a'}>a</option> | |
273 | <option value='b' $selected{'HW_MODE'}{'b'}>b</option> | |
274 | <option value='g' $selected{'HW_MODE'}{'g'}>g</option> | |
275 | </select> | |
276 | </td></tr> | |
277 | ||
278 | <tr><td width='25%' class='base'>Encryption: </td><td class='base' colspan='3'> | |
279 | <select name='ENC'> | |
280 | <option value='none' $selected{'ENC'}{'none'}>none</option> | |
281 | <option value='wpa1' $selected{'ENC'}{'wpa1'}>wpa1</option> | |
282 | <option value='wpa2' $selected{'ENC'}{'wpa2'}>wpa2</option> | |
283 | </select> | |
284 | </td></tr> | |
285 | <tr><td width='25%' class='base'>Channel: </td><td class='base' colspan='3'> | |
286 | <select name='CHANNEL'> | |
287 | END | |
288 | ; | |
289 | foreach $channel (@channellist){ | |
290 | print "<option $selected{'CHANNEL'}{$channel}>$channel</option>"; | |
291 | } | |
292 | ||
293 | print <<END | |
294 | </select></td></tr> | |
295 | <tr><td width='25%' class='base'>Tx Power: </td><td class='base' colspan='3'><select name='TXPOWER'> | |
296 | END | |
297 | ; | |
298 | foreach $txpower (@txpower){ | |
299 | print "<option $selected{'TXPOWER'}{$txpower}>$txpower</option> dBm"; | |
300 | } | |
301 | print <<END | |
302 | </select></td></tr> | |
303 | <tr><td width='25%' class='base'>Passphrase: </td><td class='base' colspan='3'><input type='text' name='PWD' size='63' value='$wlanapsettings{'PWD'}' /></td></tr> | |
304 | <tr><td width='25%' class='base'>Loglevel (hostapd): </td><td class='base' width='25%'> | |
305 | <select name='SYSLOGLEVEL'> | |
306 | <option value='0' $selected{'SYSLOGLEVEL'}{'0'}>0 (verbose)</option> | |
307 | <option value='1' $selected{'SYSLOGLEVEL'}{'1'}>1 (debugging)</option> | |
308 | <option value='2' $selected{'SYSLOGLEVEL'}{'2'}>2 (informations)</option> | |
309 | <option value='3' $selected{'SYSLOGLEVEL'}{'3'}>3 (notifications)</option> | |
310 | <option value='4' $selected{'SYSLOGLEVEL'}{'4'}>4 (warnings)</option> | |
311 | </select> | |
312 | </td> | |
313 | <td width='25%' class='base'>Debuglevel (hostapd): </td><td class='base' width='25%'> | |
314 | <select name='DEBUG'> | |
315 | <option value='0' $selected{'DEBUG'}{'0'}>0 (verbose)</option> | |
316 | <option value='1' $selected{'DEBUG'}{'1'}>1 (debugging)</option> | |
317 | <option value='2' $selected{'DEBUG'}{'2'}>2 (informations)</option> | |
318 | <option value='3' $selected{'DEBUG'}{'3'}>3 (notifications)</option> | |
319 | <option value='4' $selected{'DEBUG'}{'4'}>4 (warnings)</option> | |
320 | </select> | |
321 | </td></tr> | |
322 | </table> | |
323 | END | |
324 | ; | |
325 | if ( $wlanapsettings{'INTERFACE'} =~ /green0/ ){ | |
326 | print <<END | |
327 | <br /> | |
328 | <table width='95%' cellspacing='0'> | |
329 | <td width='25%' class='base'>Mac Filter: </td><td class='base' width='25%'> | |
330 | <select name='MACMODE'> | |
331 | <option value='0' $selected{'MACMODE'}{'0'}>0 (off)</option> | |
332 | <option value='1' $selected{'MACMODE'}{'1'}>1 (Accept MACs)</option> | |
333 | <option value='2' $selected{'MACMODE'}{'2'}>2 (Deny MACs)</option> | |
334 | </select> | |
335 | </td><td colspan='2'>Mac Adress List (one per line)<br /><textarea name='MACS' cols='20' rows='5' wrap='off'> | |
336 | END | |
337 | ; | |
338 | print `cat /var/ipfire/wlanap/macfile`; | |
339 | print <<END | |
340 | </textarea></td> | |
341 | </table> | |
342 | END | |
343 | ; | |
344 | } | |
345 | print <<END | |
346 | <br /> | |
347 | <table width='10%' cellspacing='0'> | |
348 | <tr><td align='center'><form method='post' action='$ENV{'SCRIPT_NAME'}'> | |
349 | <input type='hidden' name='ACTION' value=$Lang::tr{'save'} /> | |
350 | <input type='image' alt='$Lang::tr{'save'}' title='$Lang::tr{'save'}' src='/images/media-floppy.png' /></form></td> | |
351 | </tr> | |
352 | </table> | |
353 | END | |
354 | ; | |
355 | ||
356 | if ( $wlanapsettings{'DRIVER'} eq 'MADWIFI' ){ | |
357 | $status = `wlanconfig $wlanapsettings{'INTERFACE'} list`; | |
358 | } | |
359 | if ( $wlanapsettings{'DRIVER'} eq 'NL80211' ){ | |
360 | $status = `iw dev $wlanapsettings{'INTERFACE'} station dump`; | |
361 | } | |
362 | print <<END | |
363 | <br /> | |
364 | <table width='95%' cellspacing='0'> | |
365 | <tr><td bgcolor='$color{'color20'}' colspan='2' align='left'><b>WLAN Status</b></td></tr> | |
366 | <tr><td><pre>@channellist_cmd</pre></td><td><pre>@txpower_cmd</pre></td></tr> | |
367 | <tr><td colspan='2'><pre>$status</pre></td></tr> | |
368 | </table> | |
369 | END | |
370 | ; | |
371 | &Header::closebox(); | |
372 | print "</form>"; | |
373 | &Header::closebigbox(); | |
374 | &Header::closepage(); | |
375 | ||
376 | sub WriteConfig_hostapd{ | |
377 | $wlanapsettings{'DRIVER_HOSTAPD'} = lc($wlanapsettings{'DRIVER'}); | |
378 | ||
379 | open (CONFIGFILE, ">/var/ipfire/wlanap/hostapd.conf"); | |
380 | print CONFIGFILE <<END | |
381 | ######################### basic hostapd configuration ########################## | |
382 | # | |
383 | interface=$wlanapsettings{'INTERFACE'} | |
384 | driver=$wlanapsettings{'DRIVER_HOSTAPD'} | |
385 | channel=$wlanapsettings{'CHANNEL'} | |
386 | hw_mode=$wlanapsettings{'HW_MODE'} | |
387 | logger_syslog=-1 | |
388 | logger_syslog_level=$wlanapsettings{'SYSLOGLEVEL'} | |
389 | logger_stdout=-1 | |
390 | logger_stdout_level=$wlanapsettings{'DEBUG'} | |
391 | dump_file=/tmp/hostapd.dump | |
392 | auth_algs=1 | |
393 | ctrl_interface=/var/run/hostapd | |
394 | ctrl_interface_group=0 | |
395 | END | |
396 | ; | |
397 | if ( $wlanapsettings{'HIDESSID'} eq 'on' ){ | |
398 | print CONFIGFILE <<END | |
399 | ssid=$wlanapsettings{'SSID'} | |
400 | ignore_broadcast_ssid=2 | |
401 | END | |
402 | ; | |
403 | ||
404 | }else{ | |
405 | print CONFIGFILE <<END | |
406 | ssid=$wlanapsettings{'SSID'} | |
407 | ignore_broadcast_ssid=0 | |
408 | END | |
409 | ; | |
410 | ||
411 | } | |
412 | ||
413 | if ( $wlanapsettings{'ENC'} eq 'wpa1'){ | |
414 | print CONFIGFILE <<END | |
415 | ######################### wpa hostapd configuration ############################ | |
416 | # | |
417 | wpa=1 | |
418 | wpa_passphrase=$wlanapsettings{'PWD'} | |
419 | wpa_key_mgmt=WPA-PSK | |
420 | wpa_pairwise=CCMP TKIP | |
421 | END | |
422 | ; | |
423 | }elsif ( $wlanapsettings{'ENC'} eq 'wpa2'){ | |
424 | print CONFIGFILE <<END | |
425 | ######################### wpa hostapd configuration ############################ | |
426 | # | |
427 | wpa=2 | |
428 | wpa_passphrase=$wlanapsettings{'PWD'} | |
429 | wpa_key_mgmt=WPA-PSK | |
430 | wpa_pairwise=CCMP TKIP | |
431 | END | |
432 | ; | |
433 | } | |
434 | close CONFIGFILE; | |
435 | ||
436 | open (MACFILE, ">/var/ipfire/wlanap/macfile"); | |
437 | foreach(@macs){ | |
438 | $_ =~ s/\r//gi; | |
439 | chomp($_); | |
440 | if ( $_ ne "" ){print MACFILE $_;} | |
441 | } | |
442 | close MACFILE; | |
443 | } |