2 # Functions for configuring portsentry, hostsentry and logcheck
4 BEGIN { push(@INC, ".."); };
8 # get_portsentry_config()
9 # Parses the portsentry.conf file
10 sub get_portsentry_config
12 return &get_config($config{'portsentry_config'});
15 # get_hostsentry_config()
16 # Parses the hostsentry.conf file
17 sub get_hostsentry_config
19 return &get_config($config{'hostsentry_config'});
22 # get_logcheck_config()
23 # Parses the logcheck.sh program script
24 sub get_logcheck_config
26 return &get_config($config{'logcheck'});
29 # lock_config_files(&config)
32 foreach $f (&unique(map { $_->{'file'} } @{$_[0]})) {
37 # unlock_config_files(&config)
38 sub unlock_config_files
40 foreach $f (&unique(map { $_->{'file'} } @{$_[0]})) {
48 local (@rv, $lnum = 0);
50 local @lines = <CONF>;
55 if (/^([^=\s]+)\s*=\s*"(.*)"/ || /^([^=\s]+)\s*=\s*(\S+)/) {
56 push(@rv, { 'name' => $1,
61 elsif (/^\.\s+(\S+)/) {
63 local $inc = &get_config("$1");
71 # save_config(&conf, name, value)
74 local $old = &find($_[1], $_[0]);
75 local $lref = &read_file_lines($old ? $old->{'file'} : $_[0]->[0]->{'file'});
76 local $nl = "$_[1]=\"$_[2]\"";
78 $lref->[$old->{'line'}] = $nl;
88 foreach $c (@{$_[1]}) {
89 if (lc($c->{'name'}) eq lc($_[0])) {
96 # find_value(name, &config, subs)
99 local $rv = &find($_[0], $_[1]);
100 return undef if (!defined($rv));
101 local $str = $rv->{'value'};
104 while($str =~ /\$([A-z0-9\_]+)/ && !$donevar{$1}) {
106 local $val = &find_value($1, $_[1]);
107 $str =~ s/\$([A-z0-9\_]+)/$val/;
113 # get_portsentry_pids()
114 sub get_portsentry_pids
116 if ($config{'portsentry_pid'}) {
119 if (open(PID, $config{'portsentry_pid'}) && chop($pid = <PID>) &&
129 # Just see if the process is running
130 return grep { $_ != $$ } &find_byname("portsentry");
134 # portsentry_start_cmd()
135 sub portsentry_start_cmd
137 return $config{'portsentry_start'} ? $config{'portsentry_start'} :
138 "$config{'portsentry'} -$config{'portsentry_tmode'} && $config{'portsentry'} -$config{'portsentry_umode'}";
145 if ($config{'portsentry_stop'}) {
146 local $out = &backquote_logged("($config{'portsentry_stop'}) 2>&1 </dev/null");
147 return "<tt>$out</tt>" if ($out =~ /error|failed/i);
150 local @pids = &get_portsentry_pids();
152 &kill_logged("TERM", @pids) ||
153 return &text('portsentry_ekill', join(" ", @pids), $!);
156 return $text{'portsentry_estopped'};
163 # Starts portsentry, and returns an error message on failure, or undef
166 local $cmd = &portsentry_start_cmd();
167 local $out = &backquote_logged("$cmd 2>&1 </dev/null");
168 return "<tt>$out</tt>" if ($out =~ /failed|error/i);
172 # list_hostsentry_modules($conf)
173 # Returns a list of all hostsentry python modules
174 sub list_hostsentry_modules
176 local $dir = &find_value("MODULE_PATH", $_[0]);
178 local @rv = map { /^(\S+)\.py$/; $1 }
179 grep { /\.py$/ && !/^moduleExample/ } readdir(DIR);
184 # hostsentry_start_cmd()
185 sub hostsentry_start_cmd
187 return $config{'hostsentry_start'} ? $config{'hostsentry_start'}
188 : "python $config{'hostsentry'}";
192 # Start hostsentry, or return an error message
195 local $cmd = &hostsentry_start_cmd();
196 local $temp = &tempname();
197 &system_logged("$cmd >$temp 2>&1 </dev/null");
200 while(<TEMP>) { $out .= $_; }
203 return "<tt>$out</tt>" if ($out =~ /failed|error/i);
208 # Stop hostsentry, or return an error message
211 if ($config{'hostsentry_stop'}) {
212 local $out = &backquote_logged("($config{'hostsentry_stop'}) 2>&1 </dev/null")
214 return "<tt>$out</tt>" if ($out =~ /error|failed/i);
217 local $pid = &get_hostsentry_pid();
219 &kill_logged("TERM", $pid) ||
220 return &text('hostsentry_ekill', $pid, $!);
223 return $text{'hostsentry_estopped'};
229 # get_hostsentry_pid()
230 sub get_hostsentry_pid
232 local ($pid) = grep { $_ != $$ } &find_byname("python.*hostsentry");
236 # get_hostsentry_dir()
237 sub get_hostsentry_dir
239 $config{'hostsentry_config'} =~ /^(\S+)\//;