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

package ProFtpconf;
use MDK::Common;
use strict;
require "__WIZ_HOME__/common/scripts/Vareqval.pm";

sub	true_or_false {
    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	do_it {
    my %mdk = Vareqval->get("/etc/sysconfig/mdk_serv");
    $mdk{wiz_ftp_external} = $ENV{wiz_ftp_external}	if defined $ENV{wiz_ftp_external} or
	die "wiz_ftp_external not defined in env !";
    $mdk{wiz_ftp_internal} = $ENV{wiz_ftp_internal}	if defined $ENV{wiz_ftp_internal} or 
	die "wiz_ftp_internal not defined in env !";
    $mdk{wiz_ftp_external} = true_or_false($mdk{wiz_ftp_external});
    $mdk{wiz_ftp_internal} = $mdk{wiz_ftp_external} ? 1 : true_or_false($mdk{wiz_ftp_internal});
    my $wiz_ftp_internal = $mdk{wiz_ftp_internal};
    my $wiz_ftp_external = $mdk{wiz_ftp_external};
    my $wiz_device = $mdk{wiz_device}			if defined $mdk{wiz_device} or
	die "wiz_device not in /etc/sysconfig/mdk_serv";
    Vareqval->commit("/etc/sysconfig/mdk_serv", \%mdk);

    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) {
	my %mdk = Vareqval->get("/etc/sysconfig/network-scripts/ifcfg-".$wiz_device);
	my $wiz_ip_net = $mdk{NETWORK}		if defined $mdk{NETWORK} or 
	    die "NETWORK not in /etc/sysconfig/network-scripts/ifcfg-".$wiz_device;
	($wiz_ip_net) = ($wiz_ip_net =~ /(.*)\..*/);
	$allow = "$wiz_ip_net.";
    }
    elsif (!$wiz_ftp_external) {
	$allow = "none";
    }
    my $file = "/etc/proftpd.conf";
    open (NEW, "< $file");
    my $exist = 0;
    while (<NEW>) {
	if (m/^\s*<Global>/s...m/^\s*<\/Global>/s ) {
	    if (m/^\s*<Limit LOGIN>/s...m/^\s*<\/Limit>/s ) {
		if (/^\s*(?!\#)\s*Order .*$/) {
		    $exist += 1;
		}
		if (/^\s*(?!\#)\s*Allow .*$/) {
		    $exist += 1;
		}
		if (/^\s*(?!\#)\s*Deny .*$/) {
		    $exist += 1;
		}
	    }
	}
    }
    close (NEW);
    if ($exist < 3) {
	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 '
<Global>
  <Limit LOGIN>
    Order allow,deny
    Allow from ' .$allow.'
    Deny from all
  </Limit>
</Global>';
	close NEW;
    }
    else {
	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;