]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/upnp.cgi
eb635bb5bd9a90e464fef3ff60d6839d4a0dbd96
[people/pmueller/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 &Header::showhttpheaders();
28 ############################################################################################################################
29 ############################################### Setzen von Standartwerten ##################################################
30
31 $upnpsettings{'DEBUGMODE'} = '3';
32 $upnpsettings{'FORWARDRULES'} = 'yes';
33 $upnpsettings{'DOWNSTREAM'} = '1048576';
34 $upnpsettings{'UPSTREAM'} = '131072';
35 $upnpsettings{'DESCRIPTION'} = 'gatedesc.xml';
36 $upnpsettings{'XML'} = '/etc/linuxigd';
37 $upnpsettings{'ENABLED'} = 'off';
38 $upnpsettings{'friendlyName'} = 'IPFire Gateway';
39 ### Values that have to be initialized
40 $upnpsettings{'ACTION'} = '';
41
42 &General::readhash("${General::swroot}/upnp/settings", \%upnpsettings);
43 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
44 &Header::getcgihash(\%upnpsettings);
45
46 &Header::openpage('UPnP', 1, '');
47 &Header::openbigbox('100%', 'left', '', $errormessage);
48
49 ############################################################################################################################
50 ################################################### Speichern der Config ###################################################
51
52 if ($upnpsettings{'ACTION'} eq $Lang::tr{'save'})
53 {
54 $upnpsettings{'DOWNSTREAM'} = $upnpsettings{'DOWNSTREAM'} * 1024;
55 $upnpsettings{'UPSTREAM'} = $upnpsettings{'UPSTREAM'} * 1024;
56 &General::writehash("${General::swroot}/upnp/settings", \%upnpsettings);
57
58 open (FILE, ">${General::swroot}/upnp/upnpd.conf") or die "Can't save the upnp config: $!";
59 flock (FILE, 2);
60
61 print FILE <<END
62
63 # UPnP Config by Ipfire Project
64
65 debug_mode = $upnpsettings{'DEBUGMODE'}
66 insert_forward_rules = $upnpsettings{'FORWARDRULES'}
67 forward_chain_name = FORWARD
68 prerouting_chain_name = PORTFW
69 upstream_bitrate = $upnpsettings{'DOWNSTREAM'}
70 downstream_bitrate = $upnpsettings{'UPSTREAM'}
71 description_document_name = $upnpsettings{'DESCRIPTION'}
72 xml_document_path = $upnpsettings{'XML'}
73
74 END
75 ;
76 close FILE;
77 system("/usr/local/bin/upnpctrl upnpxml $upnpsettings{'XML'} $upnpsettings{'DESCRIPTION'} $upnpsettings{'manufacturer'}");
78 }
79 elsif ($upnpsettings{'ACTION'} eq 'Start')
80 {
81 $upnpsettings{'ENABLED'} = 'on';
82 &General::writehash("${General::swroot}/upnp/settings", \%upnpsettings);
83 system("/usr/local/bin/upnpctrl upnpdstart $netsettings{'RED_DEV'} $netsettings{'GREEN_DEV'}");
84 }
85 elsif ($upnpsettings{'ACTION'} eq 'Stop')
86 {
87 $upnpsettings{'ENABLED'} = 'off';
88 &General::writehash("${General::swroot}/upnp/settings", \%upnpsettings);
89 system("/usr/local/bin/upnpctrl stop");
90 }
91 elsif ($upnpsettings{'ACTION'} eq $Lang::tr{'restart'})
92 {
93 &General::writehash("${General::swroot}/upnp/settings", \%upnpsettings);
94 system("/usr/local/bin/upnpctrl stop");
95 system("/usr/local/bin/upnpctrl start $netsettings{'RED_DEV'} $netsettings{'GREEN_DEV'}");
96 }
97
98 &General::readhash("${General::swroot}/upnp/settings", \%upnpsettings);
99 $upnpsettings{'DOWNSTREAM'} = $upnpsettings{'DOWNSTREAM'} / 1024;
100 $upnpsettings{'UPSTREAM'} = $upnpsettings{'UPSTREAM'} / 1024;
101
102 if ($errormessage)
103 {
104 &Header::openbox('100%', 'left', $Lang::tr{'error messages'});
105 print "<class name='base'>$errormessage\n";
106 print "&nbsp;</class>\n";
107 &Header::closebox();
108 }
109
110 ############################################################################################################################
111 ############################################################################################################################
112
113 &Header::openbox('100%', 'center', 'UPnP');
114 print <<END
115 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
116 <table width='95%' cellspacing='0'>
117 END
118 ;
119 if ( $message ne "" ) {print "<tr><td colspan='3' align='center'><font color='red'>$message</font>";}
120
121 my $lines = 0;
122 my $key = '';
123 foreach $key (sort keys %servicenames)
124 {
125 if ($lines % 2)
126 {print "<tr bgcolor='${Header::table1colour}'>\n";}
127 else
128 {print "<tr bgcolor='${Header::table2colour}'>\n"; }
129
130 print "<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='${Header::table1colour}'><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 }