2 # Functions for parsing xinetd config files
4 BEGIN { push(@INC, ".."); };
11 return &parse_xinetd($config{'xinetd_conf'});
14 # parse_xinetd(file, [offset])
15 # Parses the xinetd config file into directives
20 local @lines = <INET>;
25 s/\r|\n//g; s/#.*$//g; s/\s+$//; # remove newlines, comments and spaces
27 # put subsequent directives into a section
29 $section->{'members'} = [ ];
32 # finished the section
33 $section->{'eline'} = $lnum;
36 elsif (/^\s*include\s+(\S+)/) {
37 # include directives from a file
38 push(@rv, &parse_xinetd($1, scalar(@rv)));
40 elsif (/^\s*includedir\s+(\S+)/) {
41 # include directives from every file in a directory
44 foreach $f (readdir(DIR)) {
45 next if ($f =~ /^\./);
46 push(@rv, &parse_xinetd("$d/$f", scalar(@rv)));
50 elsif (/^\s*(\S+)\s*(.*)/) {
51 # a directive or start of a section
52 local $dir = { 'name' => $1, 'file' => $_[0],
53 'index' => scalar(@rv) + $_[1],
54 'line' => $lnum, 'eline' => $lnum };
56 if ($v =~ /^(=|\+=|-=)\s+(.*)/) {
61 $dir->{'op'} = '=' if ($service);
64 local @v = split(/\s+/, $dir->{'value'});
65 $dir->{'values'} = \@v;
67 push(@{$section->{'members'}}, $dir);
68 local @q = @{$section->{'quick'}->{$dir->{'name'}}};
69 if ($dir->{'op'} eq '=') {
72 elsif ($dir->{'op'} eq '+=') {
75 elsif ($dir->{'op'} eq '-=') {
76 @q = grep { &indexof($_, @v) < 0 } @q;
78 $section->{'quick'}->{$dir->{'name'}} = \@q;
80 else { push(@rv, $dir); }
87 # set_member_value(&xinet, name, [value]*)
88 # Removes all members with the given name and replaces them with one
92 local @m = @{$_[0]->{'members'}};
93 @m = grep { $_->{'name'} ne $_[1] } @m;
95 push(@m, { 'name' => $_[1],
97 'values' => [ @_[2..@_-1] ] } );
98 $_[0]->{'quick'}->{$_[1]} = [ @_[2..@_-1] ];
101 delete($_[0]->{'quick'}->{$_[1]});
103 $_[0]->{'members'} = \@m;
106 # create_xinet(&xinet, [file])
107 # Add a new xinet record to the end of the config file
110 local $lref = &read_file_lines($_[1] || $config{'xinetd_conf'});
111 push(@$lref, &xinet_lines($_[0]));
115 # modify_xinet(&xinet)
116 # Update an existing xinet record
119 local $lref = &read_file_lines($_[0]->{'file'});
120 splice(@$lref, $_[0]->{'line'}, $_[0]->{'eline'} - $_[0]->{'line'} + 1,
121 &xinet_lines($_[0]));
125 # delete_xinet(&xinet)
126 # Delete an existing xinet record
129 local $lref = &read_file_lines($_[0]->{'file'});
130 splice(@$lref, $_[0]->{'line'}, $_[0]->{'eline'} - $_[0]->{'line'} + 1);
134 # xinet_lines(&xinet, tabs)
138 $rv[0] = "$_[1]$_[0]->{'name'}";
139 $rv[0] .= " $_[0]->{'op'}" if ($_[0]->{'op'});
140 foreach $v (@{$_[0]->{'values'}}) {
143 if ($_[0]->{'members'}) {
144 push(@rv, $_[1].'{');
145 foreach $m (@{$_[0]->{'members'}}) {
146 push(@rv, &xinet_lines($m, "$_[1]\t"));
148 push(@rv, $_[1].'}');
154 # Returns a list of supported protocols on this system
158 open(PROT, $config{'protocols_file'});
169 sub is_xinetd_running
171 if ($config{'pid_file'}) {
172 return &check_pid_file($config{'pid_file'});
175 local ($pid) = &find_byname("xinetd");