2 # Library for TCP Wrappers
4 BEGIN { push(@INC, ".."); };
8 # list_rules($filename)
9 # Parse rules from /etc/hosts.*
10 # File format described in "man 5 hosts_access"
16 open(HOSTS, $file) || return ();
20 while ($line = <HOSTS>) {
21 my ($slnum, $elnum) = ($lnum, $lnum);
24 while ($line =~ /^(.*)\\/) {
25 # Continuation line! Read the next one and append it
33 if ($line =~ /^\#(.*)/) {
35 $cmt = $cmt ? $cmt."\n".$1 : $1;
36 } elsif ($line =~ /^\s*$/) {
39 my @cmtlines = split(/\n/, $cmt);
41 my ($service, $host, $cmd) = split /:/, $line, 3;
42 $service =~ s/^\s*//; $service =~ s/\s*$//;
43 $host =~ s/^\s*//; $host =~ s/\s*$//;
45 push @ret, { 'id' => $id++,
46 'service' => $service,
49 'line' => $slnum-scalar(@cmtlines),
61 # List system services from (x)inetd or return ()
65 if (&foreign_installed("xinetd")) {
66 &foreign_require("xinetd", "xinetd-lib.pl");
67 my @conf = &foreign_call('xinetd', 'get_xinetd_config');
69 next if ($x->{'quick'}{'server'}[0] !~ /\/([^\/]+)$/);
72 } elsif (&foreign_installed("inetd")) {
73 &foreign_require("inetd", "inetd-lib.pl");
74 my @conf = &foreign_call('inetd', 'list_inets');
76 next unless ($x->[9] =~ /^(\S+)/);
84 # delete_rule($filename, &rule)
85 # Removes one rule entry from the file
87 my ($filename, $rule) = @_;
89 my $lref = &read_file_lines($filename);
90 my $len = $rule->{'eline'} - $rule->{'line'} + 1;
91 splice(@$lref, $rule->{'line'}, $len);
92 &flush_file_lines($filename);
95 # create_rule($filename, &rule)
98 my ($file, $rule) = @_;
100 my $lref = &read_file_lines($file);
101 my $newline = $rule->{'service'}.' : '.$rule->{'host'}.($rule->{'cmd'} ? ' : '.$rule->{'cmd'} : '');
102 push(@$lref, $newline);
103 &flush_file_lines($file);
106 # modify_rule($filename, &old_rule, &new_rule)
109 my ($filename, $oldrule, $newrule) = @_;
111 my @newline = ($newrule->{'service'}.' : '.$newrule->{'host'}.($newrule->{'cmd'} ? ' : '.$newrule->{'cmd'} : ''));
113 my $lref = &read_file_lines($filename);
114 my $len = $oldrule->{'eline'} - $oldrule->{'line'} + 1;
115 splice(@$lref, $oldrule->{'line'}, $len, @newline);
116 &flush_file_lines($filename);