Handle hostnames with upper-case letters
[webmin.git] / stunnel / edit_stunnel.cgi
1 #!/usr/local/bin/perl
2 # edit_stunnel.cgi
3 # Edit or create an SSL tunnel run from inetd
4
5 require './stunnel-lib.pl';
6 &ReadParse();
7
8 if ($in{'new'}) {
9         &ui_print_header(undef, $text{'create_title'}, "");
10         $st = { 'active' => 1 };
11         }
12 else {
13         &ui_print_header(undef, $text{'edit_title'}, "");
14         @stunnels = &list_stunnels();
15         $st = $stunnels[$in{'idx'}];
16         }
17
18 print "<form action=save_stunnel.cgi>\n";
19 print "<input type=hidden name=new value='$in{'new'}'>\n";
20 print "<input type=hidden name=idx value='$in{'idx'}'>\n";
21
22 print "<table border width=100%>\n";
23 print "<tr $tb> <td><b>$text{'edit_header1'}</b></td> </tr>\n";
24 print "<tr $cb> <td><table width=100%>\n";
25
26 print "<tr> <td><b>$text{'edit_name'}</b></td>\n";
27 printf "<td><input name=name size=15 value='%s'></td>\n",
28         $st->{'name'};
29
30 print "<td><b>$text{'edit_port'}</b></td>\n";
31 printf "<td><input name=port size=6 value='%s'></td> </tr>\n",
32         $st->{'port'};
33
34 print "<tr> <td><b>$text{'edit_active'}</b></td>\n";
35 printf "<td><input type=radio name=active value=1 %s> %s\n",
36         $st->{'active'} ? "checked" : "", $text{'yes'};
37 printf "<input type=radio name=active value=0 %s> %s</td>\n",
38         $st->{'active'} ? "" : "checked", $text{'no'};
39
40 if (!$in{'new'}) {
41         print "<td><b>$text{'edit_type'}</b></td>\n";
42         print "<td><tt>$st->{'type'}</tt></td>\n";
43         }
44 elsif ($has_inetd && $has_xinetd) {
45         print "<td><b>$text{'edit_type'}</b></td>\n";
46         print "<td><select name=type>\n";
47         print "<option selected>xinetd\n";
48         print "<option>inetd\n";
49         print "</select></td>\n";
50         }
51 print "</tr>\n";
52
53 print "</table></td></tr></table><br>\n";
54 print "<table border width=100%>\n";
55 print "<tr $tb> <td><b>$text{'edit_header2'}</b></td> </tr>\n";
56 print "<tr $cb> <td><table width=100%>\n";
57
58 if ($in{'new'}) {
59         $ptymode = 'l';
60         }
61 elsif (&get_stunnel_version() >= 4) {
62         # Parse new-style stunnel configuration file
63         if ($st->{'args'} =~ /^(\S+)\s+(\S+)/) {
64                 $cfile = $2;
65                 @conf = &get_stunnel_config($cfile);
66                 ($conf) = grep { !$_->{'name'} } @conf;
67                 if ($cmd = $conf->{'values'}->{'exec'}) {
68                         $args = $conf->{'values'}->{'execargs'};
69                         $ptymode = $conf->{'values'}->{'pty'} eq 'yes' ? "L"
70                                                                        : "l";
71                         }
72                 else {
73                         $rport = $conf->{'values'}->{'connect'};
74                         if ($rport =~ /^(\S+):(\d+)/) {
75                                 $rhost = $1;
76                                 $rport = $2;
77                                 }
78                         }
79                 $pem = $conf->{'values'}->{'cert'};
80                 $cmode = $conf->{'values'}->{'client'} =~ /yes/i;
81                 $tcpw = $conf->{'values'}->{'service'};
82                 $iface = $conf->{'values'}->{'local'};
83                 }
84         }
85 else {
86         # Parse old-style stunnel parameters
87         if ($st->{'args'} =~ s/\s*-([lL])\s+(\S+)\s+--\s+(.*)// ||
88             $st->{'args'} =~ s/\s*-([lL])\s+(\S+)//) {
89                 $ptymode = $1;
90                 $cmd = $2;
91                 $args = $3;
92                 }
93         if ($st->{'args'} =~ s/\s*-r\s+((\S+):)?(\d+)//) {
94                 $rhost = $2;
95                 $rport = $3;
96                 }
97         if ($st->{'args'} =~ s/\s*-p\s+(\S+)//) {
98                 $pem = $1;
99                 }
100         if ($st->{'args'} =~ s/\s*-c//) {
101                 $cmode = 1;
102                 }
103         if ($st->{'args'} =~ s/\s*-N\s+(\S+)//) {
104                 $tcpw = $1;
105                 }
106         if ($st->{'args'} =~ s/\s*-I\s+(\S+)//) {
107                 $iface = $1;
108                 }
109         }
110
111 printf "<tr> <td><input type=radio name=mode value=0 %s> %s</td>\n",
112         $ptymode eq 'l' ? 'checked' : '', $text{'edit_mode0'};
113 printf "<td nowrap><b>%s</b> <input name=cmd0 size=15 value='%s'>\n",
114         $text{'edit_cmd'}, $ptymode eq 'l' ? $cmd : '';
115 printf "<b>%s</b> <input name=args0 size=20 value='%s'></td> </tr>\n",
116         $text{'edit_args'}, $ptymode eq 'l' ? $args : '';
117
118 printf "<tr> <td><input type=radio name=mode value=1 %s> %s</td>\n",
119         $ptymode eq 'L' ? 'checked' : '', $text{'edit_mode1'};
120 printf "<td nowrap><b>%s</b> <input name=cmd1 size=15 value='%s'>\n",
121         $text{'edit_cmd'}, $ptymode eq 'L' ? $cmd : '';
122 printf "<b>%s</b> <input name=args1 size=20 value='%s'></td> </tr>\n",
123         $text{'edit_args'}, $ptymode eq 'L' ? $args : '';
124
125 printf "<tr> <td><input type=radio name=mode value=2 %s> %s</td>\n",
126         $rport ? 'checked' : '', $text{'edit_mode2'};
127 printf "<td nowrap><b>%s</b> <input name=rhost size=20 value='%s'>\n",
128         $text{'edit_rhost'}, !$rport ? '' : $rhost ? $rhost : 'localhost';
129 printf "<b>%s</b> <input name=rport size=6 value='%s'></td> </tr>\n",
130         $text{'edit_rport'}, $rport;
131
132 print "</table></td></tr></table><br>\n";
133 print "<table border width=100%>\n";
134 print "<tr $tb> <td><b>$text{'edit_header3'}</b></td> </tr>\n";
135 print "<tr $cb> <td><table width=100%>\n";
136
137 print "<tr> <td><b>$text{'edit_pem'}</b></td> <td nowrap>\n";
138 $haspem = $config{'pem_path'} && -r $config{'pem_path'};
139 if ($in{'new'}) {
140         printf "<input type=radio name=pmode value=0 %s> %s\n",
141                 "", $text{'edit_pem0'};
142         printf "<input type=radio name=pmode value=1 %s> %s\n",
143                 $haspem ? "" : "checked", $text{'edit_pem1'};
144         printf "<input type=radio name=pmode value=2 %s> %s\n",
145                 $haspem ? "checked" : "", $text{'edit_pem2'};
146         printf "<input name=pem size=25 value='%s'> %s</td> </tr>\n",
147                 $haspem ? $config{'pem_path'} : "", &file_chooser_button("pem");
148         }
149 else {
150         local $pmode = $pem eq $webmin_pem ? 1 :
151                        $pem ? 2 : 0;
152         printf "<input type=radio name=pmode value=0 %s> %s\n",
153                 $pmode == 0 ? "checked" : "", $text{'edit_pem0'};
154         printf "<input type=radio name=pmode value=1 %s> %s\n",
155                 $pmode == 1 ? "checked" : "", $text{'edit_pem1'};
156         printf "<input type=radio name=pmode value=2 %s> %s\n",
157                 $pmode == 2 ? "checked" : "", $text{'edit_pem2'};
158         printf "<input name=pem size=25 value='%s'> %s</td> </tr>\n",
159                 $pmode == 2 ? $pem : "", &file_chooser_button("pem");
160         }
161
162 print "<tr> <td><b>$text{'edit_tcpw'}</b></td> <td>\n";
163 printf "<input type=radio name=tcpw_def value=1 %s> %s\n",
164         $tcpw ? "" : "checked", $text{'edit_auto'};
165 printf "<input type=radio name=tcpw_def value=0 %s>\n",
166         $tcpw ? "checked" : "";
167 printf "<input name=tcpw size=15 value='%s'></td> </tr>\n", $tcpw;
168
169 print "<tr> <td><b>$text{'edit_cmode'}</b></td> <td>\n";
170 printf "<input type=radio name=cmode value=0 %s> %s\n",
171         $cmode ? "" : "checked", $text{'edit_cmode0'};
172 printf "<input type=radio name=cmode value=1 %s> %s</td> </tr>\n",
173         $cmode ? "checked" : "", $text{'edit_cmode1'};
174
175 print "<tr> <td><b>$text{'edit_iface'}</b></td> <td>\n";
176 printf "<input type=radio name=iface_def value=1 %s> %s\n",
177         $iface ? "" : "checked", $text{'edit_auto'};
178 printf "<input type=radio name=iface_def value=0 %s>\n",
179         $iface ? "checked" : "";
180 printf "<input name=iface size=25 value='%s'></td> </tr>\n", $iface;
181
182 print "</table></td></tr></table>\n";
183 print "<input type=hidden name=args value='$st->{'args'}'>\n";
184 print "<table width=100%><tr>\n";
185 if ($in{'new'}) {
186         print "<td><input type=submit value='$text{'create'}'></td>\n";
187         }
188 else {
189         print "<td><input type=submit value='$text{'save'}'></td>\n";
190         print "<td align=right><input type=submit name=delete ",
191               "value='$text{'delete'}'></td>\n";
192         }
193 print "</tr></table></form>\n";
194
195 &ui_print_footer("", $text{'index_return'});
196