summaryrefslogtreecommitdiffstats
path: root/ftp_wizard/scripts/ProFtpconf.pm
blob: 5db3c385178dea4149cbc39ac0a40ac662ee0c89 (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
#!/usr/bin/perl

# Drakwizard

# Copyright (C) 2002 MandrakeSoft Arnaud Desmons (adesmons@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 ProFtpconf;
require "__WIZ_HOME__/common/scripts/IFCFG.pm";
use MDK::Common;
use strict;
require "__WIZ_HOME__/common/scripts/Vareqval.pm";

sub	true {
    my	($val) = @_;

    $val eq "1" || $val eq "\'1\'" || $val eq "\"1\"" || 
	$val eq "true" || $val eq "\'true\'" || $val eq "\"true\"" and
	return 1;
    0;
}

sub	check_dir {
    foreach (keys %ENV) {
	print "$_ $ENV{$_}\n";
    }
    -d ($ENV{wiz_dir}) and return 10;
    1;
}

sub	get_dir {
    while (<NEW>) { # we need 3 elements to consider section as known
	if (m/^\s*<drakwizard>/s...m/^\s*<\/drakwizard>/s ) {
	    if (m/^\s*<Anonymous\s*(.*)>/s ) {
		return $1;
	    }
	}
    }
    return "";
}

my $o = IFCFG->new();

sub	check {
    $> and return 1;
    $o->is_dhcp() and return 2;
    0;
}

sub	print_anonymous() {
    print '
#<drakwizard>
<Anonymous '.$_[0].'>
  User	ftp
  Group	ftp
  UserAlias anonymous ftp
  MaxClients 10
  <Limit WRITE>
    DenyAll
  </Limit>
</Anonymous>
#</drakwizard>
';
}

sub	do_it {
    my $wiz_ftp_internal = $ENV{wiz_ftp_external} ? 1 : true $ENV{wiz_ftp_internal};
    my $wiz_ftp_external = true $ENV{wiz_ftp_external};
    my $file = "/etc/proftpd.conf";
    die "no ftp configuration file found ! warning." if (!-f $file);
    MDK::Common::cp_af($file, $file . ".orig");
    open(NEW, "< $file") or die "error while opening $file: $!";
    my $allow = "all";
    if ($wiz_ftp_internal && !$wiz_ftp_external) {
	($allow) = $o->itf_get("IPADDR") =~ qr/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.)\d{1,3}$/;
	$allow .= " 127.0.0.1";
    }
    elsif (!$wiz_ftp_external) {
	$allow = "none";
    }
    my $file = "/etc/proftpd.conf";
    open (NEW, "< $file");
    my $exist = 0;
    while (<NEW>) { # we need 3 elements to consider section as known
	if (m/^\s*<Global>/s...m/^\s*<\/Global>/s ) {
	    if (m/^\s*<Limit LOGIN>/s...m/^\s*<\/Limit>/s ) {
		if (/^\s*(?!\#)\s*Order .*$/) {
		    $exist++;
		}
		if (/^\s*(?!\#)\s*Allow .*$/) {
		    $exist++;
		}
		if (/^\s*(?!\#)\s*Deny .*$/) {
		    $exist++;
		}
	    }
	}
    }
    close (NEW);
    if ($exist < 3) { # Odd parameters are commented if exists to then add a known section
	substInFile {
	    if (m/^\s*<Global>/s...m/^\s*<\/Global>/s ) {
		if (m/^\s*<Limit LOGIN>/s...m/^\s*<\/Limit>/s ) {
		    s/^\s*(?!\#)\s*Order .*$/\#$&\n/s;
		    s/^\s*(?!\#)\s*Allow .*$/\#$&\n/s;
		    s/^\s*(?!\#)\s*Deny .*$/\#$&\n/s;
		}
	    }
	} $file;
	open (NEW, ">> $file");
	print NEW '
#<drakwizard>
<Global>
  <Limit LOGIN>
    Order allow,deny
    Allow from '.$allow.'
    Deny from all
  </Limit>
</Global>
#</drakwizard>';
	close NEW;
    }
    else { # the known section (3 parameters ) is replaced with our needs
	substInFile {
	    if (m/^\s*<Global>/s...m/^\s*<\/Global>/s ) {
		if (m/^\s*<Limit LOGIN>/s...m/^\s*<\/Limit>/s ) {
		    if (/^\s*(?!\#)\s*Order .*$/i) {
			if (!/\s*Order\s*allow,\s*deny\s*$/) {
			    s//\#$&\n    Order allow,deny\n/;
			}
		    }
		    if (/^\s*(?!\#)\s*Allow .*$/i) {
			if (!/\s*Allow\s*from\s*$allow\s*$/) {
			    s//\#$&\n    Allow from $allow/;
			}
		    }
		    if (/^\s*(?!\#)\s*Deny .*$/i) {
			if (!/\s*Deny\s*from\s*all\s*$/) {
			    s//\#$&\n    Deny from all\n/;
			}
		    }
		}
	    }
	} $file;
    }
    system("/etc/rc.d/init.d/proftpd restart");
    10;
}
1;