3 # Create a new virtual host.
5 require './apache-lib.pl';
7 $access{'create'} || &error($text{'cvirt_ecannot'});
8 &error_setup($text{'cvirt_err'});
11 # get directives from clone
12 if ($in{'clone'} ne '') {
13 $clone = $conf->[$in{'clone'}];
14 @cmems = grep { $_->{'name'} ne 'ServerName' &&
15 $_->{'name'} ne 'Port' &&
16 $_->{'name'} ne 'DocumentRoot' &&
17 $_->{'name'} ne 'ServerAlias' } @{$clone->{'members'}};
20 # Parse and find the specified address to listen on
21 if ($in{'addr_def'} == 1) {
22 if ($httpd_modules{'core'} >= 1.2) { $addr = "_default_"; }
25 elsif ($in{'addr_def'} == 2) {
28 elsif ($in{'addr'} !~ /\S/) {
29 &error($text{'cvirt_eaddr1'});
32 foreach $a (split(/\s+/, $in{'addr'})) {
33 &to_ipaddress($a) || &to_ip6address($a) ||
34 &error(&text('cvirt_eaddr2', $a));
35 push(@addrs, &check_ip6address($a) ? "[$a]" : $a);
37 $addr = join(" ", @addrs);
40 # Parse and find the specified port
41 $defport = &find_directive("Port", $conf) || 80;
42 if ($in{'port_mode'} == 0) {
46 elsif ($in{'port_mode'} == 1) {
50 elsif ($in{'port'} !~ /^\d+$/) {
51 &error(&text('cvirt_eport', $in{'port'}));
54 $port = ":$in{'port'}";
55 $portnum = $in{'port'};
58 if (!$in{'name_def'}) {
59 @names = split(/\s+/, $in{'name'});
60 @names || &error(&text('cvirt_ename', $in{'name'}));
63 # Check if the virtual server already exists
64 if (!$in{'name_def'}) {
65 $aclname = "$in{'name'}$port";
66 @virt = &find_directive_struct("VirtualHost", $conf);
68 local ($clash) = grep { $_ eq $aclname } &virt_acl_name($v);
69 $clash && &error($text{'cvirt_etaken'});
73 # Check if the root directory is allowed
74 !$in{'root'} || &allowed_auth_file($in{'root'}) ||
75 &error(&text('cvirt_eroot3', $in{'root'}));
77 if ($in{'root'} && !-d $in{'root'}) {
78 # create the document root
79 mkdir($in{'root'}, 0755) ||
80 &error(&text('cvirt_eroot2', $in{'root'}, $!));
81 $user = &find_directive("User", $conf);
82 $group = &find_directive("Group", $conf);
83 $uid = $user ? getpwnam($user) : 0;
84 $gid = $group ? getgrnam($group) : 0;
85 chown($uid, $gid, $in{'root'});
89 if ($in{'fmode'} == 0) {
90 # Use the first file in config (usually httpd.conf)
91 $vconf = &get_virtual_config();
92 $f = $vconf->[0]->{'file'};
93 for($j=0; $vconf->[$j]->{'file'} eq $f; $j++) { }
94 $l = $vconf->[$j-1]->{'eline'}+1;
96 elsif ($in{'fmode'} == 1) {
97 # Use the standard file/directory for virtual hosts
98 local $vfile = &server_root($config{'virt_file'});
100 # Just appending to a file
103 elsif ($in{'name_def'}) {
104 # No server name, so use webmin.UTIME.conf
105 $linkfile = "webmin.".time().".conf";
106 $f = "$vfile/$linkfile";
109 # Work out a filename
110 $tmpl = $config{'virt_name'} || '${DOM}.conf';
111 %hash = ( 'dom' => $in{'name'},
113 $linkfile = &substitute_template($tmpl, \%hash);
114 $f = "$vfile/$linkfile";
118 # Use a user-specified file
121 -r $f || open(FILE, ">>$f") || &error(&text('cvirt_efile', &html_escape($f), $!));
124 &lock_apache_files();
128 # Check each IP address for a needed Listen and NameVirtualHost directive
129 foreach $a (@addrs) {
130 local $ip = &to_ipaddress($a);
131 if ($in{'listen'} && $ip) {
132 # add Listen if needed
133 local @listen = &find_directive("Listen", $conf);
135 foreach $l (@listen) {
136 if ($portnum eq "*") {
137 # Look for any Listen directive that would match the IP
138 $lfound++ if ($l eq "*" ||
140 ($l =~ /^(\S+):(\d+)$/ &&
141 &to_ipaddress("$1") eq $ip) ||
142 &to_ipaddress($l) eq $ip);
145 # Look for a Listen directive that would match
146 # the specified port and IP
147 $lfound++ if (($l eq '*' && $portnum == $defport) ||
148 ($l =~ /^\*:(\d+)$/ && $portnum == $1) ||
149 ($l =~ /^0\.0\.0\.0:(\d+)$/ && $portnum == $1) ||
150 ($l =~ /^\d+$/ && $portnum == $l) ||
151 ($l =~ /^(\S+):(\d+)$/ &&
152 &to_ipaddress("$1") eq $ip &&
154 (&to_ipaddress($l) eq $ip));
157 if (!$lfound && @listen > 0) {
158 # Apache is listening on some IP addresses, but not the
161 if ($httpd_modules{'core'} >= 2) {
162 $lip = $in{'port_mode'} == 2 ? "$ip:$in{'port'}"
166 $lip = $in{'port_mode'} == 2 ? "$ip:$in{'port'}" : $ip;
168 &save_directive("Listen", [ @listen, $lip ], $conf, $conf);
172 # add NameVirtualHost if needed
173 if ($in{'nv'} && !$in{'addr_def'} && $ip) {
175 local @nv = &find_directive("NameVirtualHost", $conf);
177 $found++ if (&to_ipaddress($nv) eq $ip ||
178 $nv =~ /^(\S+):(\S+)/ &&
179 &to_ipaddress("$1") eq $ip ||
181 $nv =~ /^\*:(\d+)$/ && $1 eq $portnum ||
182 $nv =~ /^0\.0\.0\.0:(\d+)$/ && $1 eq $portnum);
185 &save_directive("NameVirtualHost", [ @nv, $ip ], $conf, $conf);
190 # Create the structure
192 $ap = join(" ", map { $_.$port } @addrs);
198 $virt = { 'name' => 'VirtualHost',
202 'members' => \@mems };
203 push(@mems, { 'name' => 'DocumentRoot',
204 'value' => "\"$in{'root'}\"" }) if ($in{'root'});
206 push(@mems, { 'name' => 'ServerName',
207 'value' => $names[0] });
209 foreach $sa (@names) {
210 push(@mems, { 'name' => 'ServerAlias',
216 if ($in{'adddir'} && $in{'root'}) {
217 # Add a <Directory> section for the root
218 push(@mems, { 'name' => 'Directory',
219 'value' => "\"$in{'root'}\"",
223 'value' => 'from all' },
224 { 'name' => 'Options',
225 'value' => '+Indexes' },
230 &save_directive_struct(undef, $virt, $conf, $conf);
233 &unlock_apache_files();
235 # Create a symlink from another dir, if requested (as in Debian)
237 &create_webfile_link($f);
240 # Make sure it was really added
241 undef(@get_config_cache);
242 $conf = &get_config();
244 foreach $v (&find_directive_struct("VirtualHost", $conf)) {
245 next if ($v->{'value'} ne $ap);
247 $nsn = &find_directive("ServerName", $v->{'members'});
248 next if ($nsn ne $names[0]);
253 &rollback_apache_config();
254 &error(&text('cvirt_emissing', $f, "../config.cgi?$module_name"));
258 &webmin_log("virt", "create", ($in{'name_def'} ? $addr : $in{'name'}).$port,
262 if ($access{'virts'} ne '*') {
263 $access{'virts'} .= " $aclname";
264 &save_module_acl(\%access);