]> git.ipfire.org Git - ipfire-2.x.git/blob - html/cgi-bin/upnp.cgi
c7e15651a56e745f1683327ed140c0a65fa810b2
[ipfire-2.x.git] / html / cgi-bin / upnp.cgi
1 #!/usr/bin/perl
2 #
3 # IPFire CGIs
4 #
5 # This code is distributed under the terms of the GPL
6 #
7 # (c) The IPFire Team
8 #
9
10 use strict;
11 # enable only the following on debugging purpose
12 #use warnings;
13 #use CGI::Carp 'fatalsToBrowser';
14
15 require '/var/ipfire/general-functions.pl';
16 require "${General::swroot}/lang.pl";
17 require "${General::swroot}/header.pl";
18
19 my %upnpsettings = ();
20 my %netsettings = ();
21 my $message = "";
22 my $errormessage = "";
23 my %selected= () ;
24
25 my %servicenames =('UPnP Daemon' => 'upnpd',);
26
27 my %color = ();
28 my %mainsettings = ();
29 &General::readhash("${General::swroot}/main/settings", \%mainsettings);
30 &General::readhash("/srv/web/ipfire/html/themes/".$mainsettings{'THEME'}."/include/colors.txt", \%color);
31
32 &Header::showhttpheaders();
33 ############################################################################################################################
34 ############################################### Setzen von Standartwerten ##################################################
35
36 $upnpsettings{'DEBUGMODE'} = '3';
37 $upnpsettings{'FORWARDRULES'} = 'yes';
38 $upnpsettings{'DOWNSTREAM'} = '1048576';
39 $upnpsettings{'UPSTREAM'} = '131072';
40 $upnpsettings{'DESCRIPTION'} = 'gatedesc.xml';
41 $upnpsettings{'XML'} = '/etc/linuxigd';
42 $upnpsettings{'ENABLED'} = 'off';
43 $upnpsettings{'friendlyName'} = 'IPFire Gateway';
44 ### Values that have to be initialized
45 $upnpsettings{'ACTION'} = '';
46
47 &General::readhash("${General::swroot}/upnp/settings", \%upnpsettings);
48 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
49 &Header::getcgihash(\%upnpsettings);
50
51 &Header::openpage('UPnP', 1, '');
52 &Header::openbigbox('100%', 'left', '', $errormessage);
53
54 ############################################################################################################################
55 ################################################### Speichern der Config ###################################################
56
57 if ($upnpsettings{'ACTION'} eq $Lang::tr{'save'})
58 {
59 $upnpsettings{'DOWNSTREAM'} = $upnpsettings{'DOWNSTREAM'} * 1024;
60 $upnpsettings{'UPSTREAM'} = $upnpsettings{'UPSTREAM'} * 1024;
61 &General::writehash("${General::swroot}/upnp/settings", \%upnpsettings);
62
63 open (FILE, ">${General::swroot}/upnp/upnpd.conf") or die "Can't save the upnp config: $!";
64 flock (FILE, 2);
65
66 print FILE <<END
67
68 # UPnP Config by Ipfire Project
69
70 debug_mode = $upnpsettings{'DEBUGMODE'}
71 insert_forward_rules = $upnpsettings{'FORWARDRULES'}
72 forward_chain_name = FORWARD
73 prerouting_chain_name = PORTFW
74 upstream_bitrate = $upnpsettings{'DOWNSTREAM'}
75 downstream_bitrate = $upnpsettings{'UPSTREAM'}
76 description_document_name = $upnpsettings{'DESCRIPTION'}
77 xml_document_path = $upnpsettings{'XML'}
78
79 END
80 ;
81 close FILE;
82 system("/usr/local/bin/upnpctrl","upnpxml","$upnpsettings{'friendlyName'}","$upnpsettings{'XML'}","$upnpsettings{'DESCRIPTION'}");
83 }
84 elsif ($upnpsettings{'ACTION'} eq 'Start')
85 {
86 $upnpsettings{'ENABLED'} = 'on';
87 &General::writehash("${General::swroot}/upnp/settings", \%upnpsettings);
88 system("/usr/local/bin/upnpctrl upnpdstart $netsettings{'RED_DEV'} $netsettings{'GREEN_DEV'}");
89 }
90 elsif ($upnpsettings{'ACTION'} eq 'Stop')
91 {
92 $upnpsettings{'ENABLED'} = 'off';
93 &General::writehash("${General::swroot}/upnp/settings", \%upnpsettings);
94 system("/usr/local/bin/upnpctrl stop");
95 }
96 elsif ($upnpsettings{'ACTION'} eq $Lang::tr{'restart'})
97 {
98 &General::writehash("${General::swroot}/upnp/settings", \%upnpsettings);
99 system("/usr/local/bin/upnpctrl stop");
100 system("/usr/local/bin/upnpctrl start $netsettings{'RED_DEV'} $netsettings{'GREEN_DEV'}");
101 }
102
103 &General::readhash("${General::swroot}/upnp/settings", \%upnpsettings);
104 $upnpsettings{'DOWNSTREAM'} = $upnpsettings{'DOWNSTREAM'} / 1024;
105 $upnpsettings{'UPSTREAM'} = $upnpsettings{'UPSTREAM'} / 1024;
106
107 if ($errormessage)
108 {
109 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
110 print "<class name='base'>$errormessage\n";
111 print "&nbsp;</class>\n";
112 &Header::closebox();
113 }
114
115 ############################################################################################################################
116 ############################################################################################################################
117
118 &Header::openbox('100%', 'center', 'UPnP');
119 print <<END
120 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
121 <table width='95%' cellspacing='0'>
122 END
123 ;
124 if ( $message ne "" ) {print "<tr><td colspan='3' align='center'><font color='red'>$message</font>";}
125
126 my $lines = 0;
127 my $key = '';
128 foreach $key (sort keys %servicenames)
129 {
130 print "<tr><td align='left'>$key\n";
131 my $shortname = $servicenames{$key};
132 my $status = &isrunning($shortname);
133 print "$status\n";
134 $lines++;
135 }
136
137 print <<END
138 <tr><td align='left'>Alle Dienste:</td><td align='center' colspan='2'>
139 <input type='submit' name='ACTION' value='Start' />
140 <input type='submit' name='ACTION' value='Stop' />
141 <input type='submit' name='ACTION' value='$Lang::tr{'restart'}' />
142 </table>
143 </form>
144 <br />
145 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
146 <table width='95%' cellspacing='0'>
147 <tr><td colspan='2' align='left' bgcolor='$color{'color20'}'><b>$Lang::tr{'options'}</b></td></tr>
148 <tr><td align='left' colspan='2'><br /></td></tr>
149 <tr><td align='left'>UPnP Device Name:</td><td><input type='text' name='friendlyName' value='$upnpsettings{'friendlyName'}' size="30" /></td></tr>
150 <tr><td align='left' colspan='2'><br /></td></tr>
151 <tr><td align='left'>Downstream in KB:</td><td><input type='text' name='DOWNSTREAM' value='$upnpsettings{'DOWNSTREAM'}' size="30" /></td></tr>
152 <tr><td align='left'>Upstream in KB:</td><td><input type='text' name='UPSTREAM' value='$upnpsettings{'UPSTREAM'}' size="30" /></td></tr>
153 <tr><td align='left' colspan='2'><br /></td></tr>
154 <tr><td colspan='2' align='center'> <input type='hidden' name='ACTION' value=$Lang::tr{'save'} />
155 <input type='image' alt=$Lang::tr{'save'} src='/images/floppy.gif' /></td></tr>
156 </table></form>
157 END
158 ;
159 &Header::closebox();
160
161 &Header::openbox('100%', 'center', 'Aktuell geoeffnete Ports');
162 my @output = qx(iptables -t nat -n -L PORTFW);
163 my ($outputline, $extip, $extport, $int);
164 my @output2;
165 print "<table>";
166 foreach $outputline (@output) {
167 if ( $outputline =~ /^DNAT/ ) {
168 @output2 = split(/ /, $outputline);
169 $extip = $output2[23];
170 $extport = $output2[29];
171 $extport =~ s/dpt://;
172 $int = "$output2[31]";
173 $int =~ s/to://;
174 print "<tr><td>$extip:$extport<td align='center'><img src='/images/forward.gif' alt='=&gt;' /><td>$int";
175
176 }
177 }
178
179 print "</table>";
180
181 &Header::closebox();
182
183 &Header::closebigbox();
184 &Header::closepage();
185
186 ############################################################################################################################
187 ############################################################################################################################
188
189 sub isrunning
190 {
191 my $cmd = $_[0];
192 my $status = "<td bgcolor='${Header::colourred}' align='center'><font color='white'><b>$Lang::tr{'stopped'}</b></font></td>";
193 my $pid = '';
194 my $testcmd = '';
195 my $exename;
196
197 $cmd =~ /(^[a-z]+)/;
198 $exename = $1;
199
200 if (open(FILE, "/var/run/${cmd}.pid"))
201 {
202 $pid = <FILE>; chomp $pid;
203 close FILE;
204 if (open(FILE, "/proc/${pid}/status"))
205 {
206 while (<FILE>)
207 {if (/^Name:\W+(.*)/) {$testcmd = $1; }}
208 close FILE;
209 if ($testcmd =~ /$exename/)
210 {$status = "<td bgcolor='${Header::colourgreen}'><font color='white'><b>$Lang::tr{'running'}</b></font></td>";}
211 }
212 }
213
214 return $status;
215 }