Handle hostnames with upper-case letters
[webmin.git] / stunnel / save_stunnel.cgi
1 #!/usr/local/bin/perl
2 # save_stunnel.cgi
3 # Save, create or delete an SSL tunnel
4
5 require './stunnel-lib.pl';
6 &ReadParse();
7 &error_setup($text{'save_err'});
8
9 if ($in{'idx'} ne '') {
10         @stunnels = &list_stunnels();
11         $st = $stunnels[$in{'idx'}];
12         %old = %$st;
13         }
14
15 if ($in{'delete'}) {
16         # Just delete from inetd.conf and xinetd.conf
17         &lock_file($st->{'file'});
18         if (&get_stunnel_version(\$dummy) >= 4) {
19                 if ($st->{'args'} =~ /^(\S+)\s+(\S+)/) {
20                         $cfile = $2;
21                         if ($cfile =~ /^\Q$module_config_directory\E\//) {
22                                 &lock_file($cfile);
23                                 unlink($cfile);
24                                 }
25                         }
26                 }
27         &delete_stunnel($st);
28         }
29 else {
30         # Validate inputs
31         $in{'name'} =~ /^[A-z][A-z0-9\_\-]+$/ || &error($text{'save_ename'});
32         $in{'port'} =~ /^\d+$/ || &error($text{'save_eport'});
33         if ($in{'pmode'} == 2) {
34                 -r $in{'pem'} || &error(&text('save_epem', $in{'pem'}));
35                 }
36         if (!$in{'tcpw_def'}) {
37                 $in{'tcpw'} =~ /^\S+$/ || &error($text{'save_etcpw'});
38                 }
39         if (!$in{'iface_def'}) {
40                 &to_ipaddress($in{'iface'}) || &to_ip6address($in{'iface'}) ||
41                         &error($text{'save_eiface'});
42                 }
43         if ($in{'mode'} == 0 || $in{'mode'} == 1) {
44                 # Running a command
45                 $cmd = $in{'mode'} == 0 ? $in{'cmd0'} : $in{'cmd1'};
46                 $args = $in{'mode'} == 0 ? $in{'args0'} : $in{'args1'};
47                 &has_command($cmd) || &error($text{'save_ecmd'});
48                 }
49         else {
50                 # Connecting to remote host and port
51                 &to_ipaddress($in{'rhost'}) || &to_ip6address($in{'rhost'}) ||
52                         &error($text{'save_erhost'});
53                 $in{'rport'} =~ /^\d+$/ || &error($text{'save_erport'});
54                 }
55
56         # Create inetd/xinetd config
57         if (&get_stunnel_version(\$dummy) >= 4) {
58                 # New-style args format
59                 if ($in{'new'}) {
60                         $cfile = "$module_config_directory/$in{'name'}.conf";
61                         unlink($cfile);
62                         $conf = { };
63                         $st = { 'args' => "$stunnel_shortname $cfile",
64                                 'command' => $config{'stunnel_path'},
65                                 'type' => $in{'type'} };
66                         }
67                 else {
68                         if ($st->{'args'} =~ /^(\S+)\s+(\S+)/) {
69                                 $cfile = $2;
70                                 @conf = &get_stunnel_config($cfile);
71                                 ($conf) = grep { !$_->{'name'} } @conf;
72                                 }
73                         }
74                 $st->{'name'} = $in{'name'};
75                 $st->{'port'} = $in{'port'};
76                 $st->{'active'} = $in{'active'};
77                 if ($in{'pmode'} == 1) {
78                         $conf->{'values'}->{'cert'} = $webmin_pem;
79                         }
80                 elsif ($in{'pmode'} == 2) {
81                         $conf->{'values'}->{'cert'} = $in{'pem'};
82                         }
83                 else {
84                         delete($conf->{'values'}->{'cert'});
85                         }
86                 $conf->{'values'}->{'client'} = $in{'cmode'} ? 'yes' : 'no';
87                 if (!$in{'tcpw_def'}) {
88                         $conf->{'values'}->{'service'} = $in{'tcpw'};
89                         }
90                 else {
91                         delete($conf->{'values'}->{'service'});
92                         }
93                 if (!$in{'iface_def'}) {
94                         $conf->{'values'}->{'local'} = $in{'iface'};
95                         }
96                 else {
97                         delete($conf->{'values'}->{'local'});
98                         }
99                 if ($in{'mode'} == 0 || $in{'mode'} == 1) {
100                         # Running a command
101                         $conf->{'values'}->{'exec'} = $cmd;
102                         $conf->{'values'}->{'execargs'} = $args if ($args);
103                         $conf->{'values'}->{'pty'} = $in{'mode'} ? 'yes' : 'no';
104                         delete($conf->{'values'}->{'connect'});
105                         }
106                 else {
107                         # Connecting to remote host and port
108                         if ($in{'rhost'} eq 'localhost') {
109                                 $conf->{'values'}->{'connect'} = $in{'rport'};
110                                 }
111                         else {
112                                 $conf->{'values'}->{'connect'} =
113                                         "$in{'rhost'}:$in{'rport'}";
114                                 }
115                         delete($conf->{'values'}->{'exec'});
116                         delete($conf->{'values'}->{'execargs'});
117                         delete($conf->{'values'}->{'pty'});
118                         }
119
120                 # Save this stunnel config file
121                 if ($in{'new'}) {
122                         &create_stunnel_service($conf, $cfile);
123                         }
124                 else {
125                         &modify_stunnel_service($conf, $cfile);
126                         }
127                 }
128         else {
129                 # Old-style args format
130                 if ($in{'new'}) {
131                         $st = { 'args' => $stunnel_shortname,
132                                 'command' => $config{'stunnel_path'},
133                                 'type' => $in{'type'} };
134                         }
135                 else {
136                         $st->{'args'} = $in{'args'};
137                         }
138                 $st->{'name'} = $in{'name'};
139                 $st->{'port'} = $in{'port'};
140                 $st->{'active'} = $in{'active'};
141                 if ($in{'pmode'} == 1) {
142                         $st->{'args'} .= " -p $webmin_pem";
143                         }
144                 elsif ($in{'pmode'} == 2) {
145                         $st->{'args'} .= " -p $in{'pem'}";
146                         }
147                 if ($in{'cmode'}) {
148                         $st->{'args'} .= " -c";
149                         }
150                 if (!$in{'tcpw_def'}) {
151                         $st->{'args'} .= " -N $in{'tcpw'}";
152                         }
153                 if (!$in{'iface_def'}) {
154                         $st->{'args'} .= " -I $in{'iface'}";
155                         }
156                 if ($in{'mode'} == 0 || $in{'mode'} == 1) {
157                         # Running a command
158                         if ($in{'mode'} == 0) {
159                                 $st->{'args'} .= " -l $cmd";
160                                 }
161                         else {
162                                 $st->{'args'} .= " -L $cmd";
163                                 }
164                         if ($args) {
165                                 $st->{'args'} .= " -- $args";
166                                 }
167                         }
168                 else {
169                         # Connecting to remote host and port
170                         if ($in{'rhost'} eq 'localhost') {
171                                 $st->{'args'} .= " -r $in{'rport'}";
172                                 }
173                         else {
174                                 $st->{'args'} .=" -r $in{'rhost'}:$in{'rport'}";
175                                 }
176                         }
177                 }
178
179         if ($in{'new'}) {
180                 &lock_create_file();
181                 &create_stunnel($st);
182                 }
183         else {
184                 &lock_file($old{'file'});
185                 &modify_stunnel(\%old, $st);
186                 }
187         }
188 &unlock_all_files();
189 &webmin_log($in{'delete'} ? "delete" : $in{'new'} ? "create" : "modify",
190             "stunnel", $st->{'name'}, $st);
191 &redirect("");
192