]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/upnp.cgi
Neue UPNP.CGI vom Christian
[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 %checked = ();
21 my %netsettings = ();
22 my $message = "";
23 my $errormessage = "";
24 my %selected= () ;
25 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
26
27 my %servicenames =
28 (
29 'UPnP Daemon' => 'upnpd',
30 );
31
32 &Header::showhttpheaders();
33 ############################################################################################################################
34 ############################################### Setzen von Standartwerten ##################################################
35
36 $upnpsettings{'DEBUGMODE'} = '3';
37 $upnpsettings{'FORWARDRULES'} = 'yes';
38 $upnpsettings{'FORWARDCHAIN'} = 'FORWARD';
39 $upnpsettings{'PREROUTINGCHAIN'} = 'PORTFW';
40 $upnpsettings{'DOWNSTREAM'} = '900000';
41 $upnpsettings{'UPSTREAM'} = '16000000';
42 $upnpsettings{'DESCRIPTION'} = 'gatedesc.xml';
43 $upnpsettings{'XML'} = '/etc/linuxigd';
44 $upnpsettings{'ENABLED'} = 'off';
45 $upnpsettings{'GREEN'} = 'on';
46 $upnpsettings{'BLUE'} = 'off';
47 ### Values that have to be initialized
48 $upnpsettings{'ACTION'} = '';
49
50 &General::readhash("${General::swroot}/upnp/settings", \%upnpsettings);
51 &Header::getcgihash(\%upnpsettings);
52
53 &Header::openpage('UPnP', 1, '');
54 &Header::openbigbox('100%', 'left', '', $errormessage);
55
56 ############################################################################################################################
57 ################################################### Speichern der Config ###################################################
58
59 if ($upnpsettings{'ACTION'} eq $Lang::tr{'save'})
60 {
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 = $upnpsettings{'FORWARDCHAIN'}
73 prerouting_chain_name = $upnpsettings{'PREROUTINGCHAIN'}
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 }
83 elsif ($upnpsettings{'ACTION'} eq 'Start')
84 {
85 $upnpsettings{'ENABLED'} = 'on';
86 &General::writehash("${General::swroot}/upnp/settings", \%upnpsettings);
87 system('/usr/local/bin/upnpctrl start');
88 }
89 elsif ($upnpsettings{'ACTION'} eq 'Stop')
90 {
91 $upnpsettings{'ENABLED'} = 'off';
92 &General::writehash("${General::swroot}/upnp/settings", \%upnpsettings);
93 system('/usr/local/bin/upnpctrl stop');
94 }
95 elsif ($upnpsettings{'ACTION'} eq $Lang::tr{'restart'})
96 {
97 &General::writehash("${General::swroot}/upnp/settings", \%upnpsettings);
98 system('/usr/local/bin/upnpctrl restart');
99 }
100
101 &General::readhash("${General::swroot}/upnp/settings", \%upnpsettings);
102
103 if ($errormessage) {
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 $checked{'GREEN'}{'on'} = '';
111 $checked{'GREEN'}{'off'} = '';
112 $checked{'GREEN'}{"$upnpsettings{'GREEN'}"} = 'checked';
113 $checked{'BLUE'}{'on'} = '';
114 $checked{'BLUE'}{'off'} = '';
115 $checked{'BLUE'}{"$upnpsettings{'BLUE'}"} = 'checked';
116
117 ############################################################################################################################
118 ############################################################################################################################
119
120 &Header::openbox('100%', 'center', 'UPnP');
121 print <<END
122 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
123 <table width='400' cellspacing='0'>
124 END
125 ;
126 if ( $message ne "" ) {
127 print "<tr><td colspan='3' align='center'><font color='red'>$message</font>";
128 }
129
130 my $lines = 0;
131 my $key = '';
132 foreach $key (sort keys %servicenames)
133 {
134 if ($lines % 2) {
135 print "<tr bgcolor='${Header::table1colour}'>\n"; }
136 else {
137 print "<tr bgcolor='${Header::table2colour}'>\n"; }
138 print "<td align='left'>$key\n";
139 my $shortname = $servicenames{$key};
140 my $status = &isrunning($shortname);
141 print "$status\n";
142 $lines++;
143 }
144 print <<END
145 <tr><td><b>Alle Dienste:</b></td><td colspan='2'>
146 <input type='submit' name='ACTION' value='Start' />
147 <input type='submit' name='ACTION' value='Stop' />
148 <input type='submit' name='ACTION' value='$Lang::tr{'restart'}' />
149 </table>
150 </form>
151 <hr />
152 <form method='post' action='$ENV{'SCRIPT_NAME'}'>
153 <table width='500'>
154 <tr><td colspan='2' align='left'><b>$Lang::tr{'options'}</b>
155 <tr><td align='left'>$Lang::tr{'interfaces'}
156 <td align='left'>&nbsp;<td><input type='checkbox' name='GREEN' $checked{'GREEN'}{'on'} /> <font size='2' color='$Header::colourgreen'><b>$Lang::tr{'green'} - $netsettings{'GREEN_DEV'}</b></font>
157 END
158 ;
159 if (&Header::blue_used()){
160 print <<END
161 <tr><td align='left'>&nbsp;<td><input type='checkbox' name='BLUE' $checked{'BLUE'}{'on'} /> <font size='2' color='$Header::colourblue'><b>$Lang::tr{'wireless'} - $netsettings{'BLUE_DEV'}</b></font>
162 END
163 ;
164 }
165 print <<END
166 </table>
167
168 <table width='95%' cellspacing='0'>
169 <tr><td align='left'>Debug Mode:</td><td><input type='text' name='DEBUGMODE' value='$upnpsettings{'DEBUGMODE'}' size="30"></input></td></tr>
170 <tr><td align='left'>Forward Rules:</td><td><input type='text' name='FORWARDRULES' value='$upnpsettings{'FORWARDRULES'}' size="30"></input></td></tr>
171 <tr><td align='left'>Forward Chain:</td><td><input type='text' name='FORWARDCHAIN' value='$upnpsettings{'FORWARDCHAIN'}' size="30"></input></td></tr>
172 <tr><td align='left'>Prerouting Chain:</td><td><input type='text' name='PREROUTINGCHAIN' value='$upnpsettings{'PREROUTINGCHAIN'}' size="30"></input></td></tr>
173 <tr><td align='left'>Down Stream:</td><td><input type='text' name='DOWNSTREAM' value='$upnpsettings{'DOWNSTREAM'}' size="30"></input></td></tr>
174 <tr><td align='left'>Up Strean:</td><td><input type='text' name='UPSTREAM' value='$upnpsettings{'UPSTREAM'}' size="30"></input></td></tr>
175 <tr><td align='left'>Description Document:</td><td><input type='text' name='DESCRIPTION' value='$upnpsettings{'DESCRIPTION'}' size="30"></input></td></tr>
176 <tr><td align='left'>XML Document:</td><td><input type='text' name='XML' value='$upnpsettings{'XML'}' size="30"></input></td></tr>
177 <tr><td colspan='2' align='center'><input type='submit' name='ACTION' value=$Lang::tr{'save'} />
178 </table></form>
179 <br></br>
180 <hr></hr>
181 END
182 ;
183 &Header::closebox();
184
185 &Header::closebigbox();
186 &Header::closepage();
187
188 ############################################################################################################################
189 ############################################################################################################################
190
191 sub isrunning
192 {
193 my $cmd = $_[0];
194 my $status = "<td bgcolor='${Header::colourred}'><font color='white'><b>$Lang::tr{'stopped'}</b></font></td>";
195 my $pid = '';
196 my $testcmd = '';
197 my $exename;
198
199 $cmd =~ /(^[a-z]+)/;
200 $exename = $1;
201
202 if (open(FILE, "/var/run/${cmd}.pid"))
203 {
204 $pid = <FILE>; chomp $pid;
205 close FILE;
206 if (open(FILE, "/proc/${pid}/status"))
207 {
208 while (<FILE>)
209 {
210 if (/^Name:\W+(.*)/) {
211 $testcmd = $1; }
212 }
213 close FILE;
214 if ($testcmd =~ /$exename/)
215 {
216 $status = "<td bgcolor='${Header::colourgreen}'><font color='white'><b>$Lang::tr{'running'}</b></font></td>";
217 }
218 }
219 }
220
221 return $status;
222 }