summaryrefslogtreecommitdiffstats
path: root/web_wizard/Apache.pm
blob: 876c7bb149dcebabcf0998278ea085648a99ad90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/usr/bin/perl

# Drakwizard

# Copyright (C) 2002, 2005 Mandrakesoft
#
# Authors: Arnaud Desmons <adesmons@mandrakesoft.com>
#          Florent Villard <warly@mandrakesoft.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

package MDK::Wizard::Apache;
use strict;

use common;
use services;
use MDK::Wizard::Wizcommon;
use MDK::Wizard::Varspaceval;

my $wiz = new MDK::Wizard::Wizcommon;

my $file = "/etc/httpd/conf/httpd.conf";
my $root;

my $config;
my $o = {
	 name => N("Web wizard"),
	 var => {
		 web_internal => '',
		 web_external => '',
		 user_mod => '',
		 user_dir => '',
		 shared_dir => ''
		},
	 needed_rpm => [ 'apache-mpm-prefork', 'apache-mod_userdir' ],
	 defaultimage => "/usr/share/wizards/web_wizard/images/apache.png",
	 init => sub {
	   if (-f $file) {
	     open my $FH, $file or die "$! ($file)";
	     local $_;
	     while (<$FH>) {
	       if (/^\s*#?\s*DocumentRoot\s+(.*)/) {
		 close($FH);
		 $root = $1;
		 last;
	       }
	     }
	     close($FH);
	   } else {
	     return 0, N("%s does not exist.", $file)
	   }
	   1
	 }
	};


$o->{pages} = {
	       welcome => {
			   name => N("Web server configuration wizard") . "\n\n" . N("This wizard will help you configuring the Web server for your network."),
			   post => sub { $wiz->check_dhcp },
			   no_back => 1,
			   next => 'config',
			  },
	       config => {
			  name => N("Web server") . "\n\n" . N("Your server can act as a Web server toward your internal network (intranet) and as a Web server for the Internet.") . "\n\n" . N("Select the kind of Web service you want to activate:") . "\n\n" . N("Don't check any box if you don't want to activate your Web server."),
			  data => [
				   { text => N("Enable the Web server for the intranet"), type => 'bool', val => \$o->{var}{web_internal} },
				   { text => N("Enable the Web server for the Internet"), type => 'bool', val => \$o->{var}{web_external} },
				  ],
			  next => 'ask_mod',
			 },
	       dhcp_warning => {
				name => N("Warning.") . "\n\n" . N("You are in dhcp, server may not work with your configuration."),
				ignore => 1,
				next => 'config',
			       },
	       ask_mod => {
			   name => N("Modules:") . "\n\n" . N("* User module: allows users to have a directory in their home directories available on your http server via http://www.yourserver.com/~user, you will be asked for the name of this directory afterward."),
			   pre => sub { $o->{var}{user_mod} = is_last_user_mod() },
			   data => [
				    { text => join("\n", warp_text(N("Allows users to get a directory in their home directories available on your http server via http://www.yourserver.com/~user."), 70)), type => 'bool', val => \$o->{var}{user_mod} },
				   ],
			   post => sub { return 'user_dir' if $o->{var}{user_mod} },
			   next => 'ask_dir',
			  },
	       user_dir => {
			    name => N("Type the name of the directory users should create in their homes (without ~/) to get it available via http://www.yourserver.com/~user"),
			    pre => sub { 
				$o->{var}{user_dir} = get_user_dir();
				if ($o->{var}{user_dir} =~ /disabled/) { 
				    $o->{var}{user_dir} = 'public_html';
				}
			    },
			    data => [
				     { label => N("user http sub-directory: ~/"), help => N("Type the name of the directory users should create in their homes (without ~/) to get it available via http://www.yourserver.com/~user"), val => \$o->{var}{user_dir} },
				    ],
			    next => 'ask_dir',
			   },
	       ask_dir => {
			   name => N("Type the path of the directory you want being the document root."),
			   pre => sub { $o->{var}{shared_dir} ||= $root },
			   data => [
				    { label => N("Document root:"),  val => \$o->{var}{shared_dir} },
				   ],
			   complete => sub {
			     if (! -d $o->{var}{shared_dir}) {
			       $::in->ask_warn(N('Error'), N('The path you entered does not exist.')); return 1; }
			     else { return 0; };
			   },
			   next => 'summary',
			  },
	       summary => {
			   name =>  N("Configuring the Web server") . "\n\n" . N("The wizard collected the following parameters needed to configure your Web server") . "\n\n" . N("To accept these values, and configure your server, click the Next button or use the Back button to correct them."),
			   pre => sub {
			     $o->{var}{internal} = $o->{var}{web_internal} ? N("enabled") : N("disabled");
			     $o->{var}{external} = $o->{var}{web_external} ? N("enabled") : N("disabled")
			   },
			   data => [
				    { label => N("Intranet web server:"), fixed_val => \$o->{var}{internal} },
				    { label => N("Internet web server:"), fixed_val => \$o->{var}{external} },
				    { label => N("Document root:"), fixed_val => \$o->{var}{shared_dir} },
				    { label => N("User directory:"), fixed_val => \$o->{var}{user_dir} },
				   ],
			   post => \&do_it,
			   next => 'end',
			  },
	       end => {
		       name => N("Congratulations") . "\n\n" . N("The wizard successfully configured your intranet/Internet Web server"),
		       end => 1,
		       next => 0,
		      },
	       error_end => {
			     name => N("Failed"),
			     data => [ { label => N("Relaunch drakwizard, and try to change some parameters.") } ],
                             no_back => 1,
                             end => 1,
                             next => 0,
                            },
	      };

sub new {
  my ($class, $conf) = @_;
  $config = $conf;
#    if ($config->{ver} == 2) {
	$file = "/etc/httpd/conf/httpd.conf";
#	$o->{needed_rpm} = [ 'apache2' ];
	$o->{var}{servicecheck} = 'httpd';
#    } else {
#	$file = "/etc/httpd/conf/httpd.conf";
#	$o->{needed_rpm} = [ 'apache-1.3' ];
#	$o->{var}{servicecheck} = 'httpd';
#    }
    bless {
	o   => $o,
    }, $class;
}

sub chg_docroot {
    my $old;
    substInFile {
	s|(^\s*#?\s*DocumentRoot\s*)(\S*).*|$1$o->{var}{shared_dir}| and $old ||= $2;
    } $file;

    substInFile {
	s|^\s*<Directory\s*$old/?>|<Directory $o->{var}{shared_dir}>|;
    } $file if $old;

    substInFile {
	s|^\s*<Directory\s*/var/www/html/?>|<Directory $o->{var}{shared_dir}>|;
    } $file;
}

sub is_user_mod {
    if ($o->{var}{user_mod}) {
	return 1;
    }
    $o->{var}{user_mod} = "disabled";
    0;
}

sub is_last_user_mod {
    my $root = get_user_dir();
    chomp($root);
    !($root eq 'disabled');
}

sub get_user_dir {
    my %conf = MDK::Wizard::Varspaceval->get($file);
    $conf{UserDir};
}

sub chg_user_dir {
    # disable by default user_mod
    if (! any { /<IfModule mod_userdir.c>/ } cat_($file)) {
	append_to_file($file, <<EOF);
# add usermod dir support
<IfModule mod_userdir.c>
    UserDir disabled
</IfModule>
    
<Directory /home/*/>
    AllowOverride FileInfo AuthConfig Limit
    Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
    <Limit GET POST OPTIONS PROPFIND>
        Order allow,deny
        Allow from all
    </Limit>
    <LimitExcept GET POST OPTIONS PROPFIND>
        Order deny,allow
        Deny from all
    </LimitExcept>
</Directory>
EOF
    }

    $root = get_user_dir();
    if ($o->{var}{user_mod}) {
	substInFile {
	    s|(\s*)UserDir.*|$1UserDir $o->{var}{user_dir}|g;
	    s|<Directory\s+/home/\*/.*|<Directory /home/\*/$o->{var}{user_dir}>|g;
	} $file;
    } else {
	substInFile {
	    s|(\s*)UserDir\s*$root|$1UserDir disabled|g;
	} $file;
    }
}

sub do_it {
    $::testing and return;
    my $in = 'interactive'->vnew('su', 'Apache');
    my $w = $in->wait_message(N("Apache server"), N("Configuring your system as Apache server ..."));

    my	$that = "localhost";
    if ($o->{var}{web_external} eq "1") {
	$that = "all";
    }
    elsif ($o->{var}{web_internal} eq "1") {
	($that) = $wiz->{net}->itf_get("IPADDR") =~ qr/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)\d{1,3}$/;
	$that .= " 127.0.0.1";
    }
    cp_af($file, $file.".orig");
    substInFile {
      if (m/^\s*<Directory.*>/s...m!^\s*</Directory>!s) {
	{ s/^\s*Allow .*$/    Allow from $that\n/s }
      }
    } $file;
    chg_docroot();
    chg_user_dir();

    if (services::is_service_running('httpd')) {
      services::restart('httpd')
      } else {
	services::start('httpd')
    }
    undef $w;
    check_started($o->{var}{servicecheck});
}

1;