Handle hostnames with upper-case letters
[webmin.git] / sentry / sentry-lib.pl
1 # sentry-lib.pl
2 # Functions for configuring portsentry, hostsentry and logcheck
3
4 BEGIN { push(@INC, ".."); };
5 use WebminCore;
6 &init_config();
7
8 # get_portsentry_config()
9 # Parses the portsentry.conf file
10 sub get_portsentry_config
11 {
12 return &get_config($config{'portsentry_config'});
13 }
14
15 # get_hostsentry_config()
16 # Parses the hostsentry.conf file
17 sub get_hostsentry_config
18 {
19 return &get_config($config{'hostsentry_config'});
20 }
21
22 # get_logcheck_config()
23 # Parses the logcheck.sh program script
24 sub get_logcheck_config
25 {
26 return &get_config($config{'logcheck'});
27 }
28
29 # lock_config_files(&config)
30 sub lock_config_files
31 {
32 foreach $f (&unique(map { $_->{'file'} } @{$_[0]})) {
33         &lock_file($f);
34         }
35 }
36
37 # unlock_config_files(&config)
38 sub unlock_config_files
39 {
40 foreach $f (&unique(map { $_->{'file'} } @{$_[0]})) {
41         &unlock_file($f);
42         }
43 }
44
45 # get_config(file)
46 sub get_config
47 {
48 local (@rv, $lnum = 0);
49 open(CONF, $_[0]);
50 local @lines = <CONF>;
51 close(CONF);
52 foreach (@lines) {
53         s/\r|\n//g;
54         s/#.*$//;
55         if (/^([^=\s]+)\s*=\s*"(.*)"/ || /^([^=\s]+)\s*=\s*(\S+)/) {
56                 push(@rv, { 'name' => $1,
57                             'value' => $2,
58                             'file' => $_[0],
59                             'line' => $lnum });
60                 }
61         elsif (/^\.\s+(\S+)/) {
62                 # Included file!
63                 local $inc = &get_config("$1");
64                 push(@rv, @$inc);
65                 }
66         $lnum++;
67         }
68 return \@rv;
69 }
70
71 # save_config(&conf, name, value)
72 sub save_config
73 {
74 local $old = &find($_[1], $_[0]);
75 local $lref = &read_file_lines($old ? $old->{'file'} : $_[0]->[0]->{'file'});
76 local $nl = "$_[1]=\"$_[2]\"";
77 if ($old) {
78         $lref->[$old->{'line'}] = $nl;
79         }
80 else {
81         push(@$lref, $nl);
82         }
83 }
84
85 # find(name, &config)
86 sub find
87 {
88 foreach $c (@{$_[1]}) {
89         if (lc($c->{'name'}) eq lc($_[0])) {
90                 return $c;
91                 }
92         }
93 return undef;
94 }
95
96 # find_value(name, &config, subs)
97 sub find_value
98 {
99 local $rv = &find($_[0], $_[1]);
100 return undef if (!defined($rv));
101 local $str = $rv->{'value'};
102 if ($_[2]) {
103         local %donevar;
104         while($str =~ /\$([A-z0-9\_]+)/ && !$donevar{$1}) {
105                 $donevar{$1}++;
106                 local $val = &find_value($1, $_[1]);
107                 $str =~ s/\$([A-z0-9\_]+)/$val/;
108                 }
109         }
110 return $str;
111 }
112
113 # get_portsentry_pids()
114 sub get_portsentry_pids
115 {
116 if ($config{'portsentry_pid'}) {
117         # Get from pid file
118         local $pid;
119         if (open(PID, $config{'portsentry_pid'}) && chop($pid = <PID>) &&
120             kill(0, $pid)) {
121                 close(PID);
122                 return ( $pid );
123                 }
124         else {
125                 return ();
126                 }
127         }
128 else {
129         # Just see if the process is running
130         return grep { $_ != $$ } &find_byname("portsentry");
131         }
132 }
133
134 # portsentry_start_cmd()
135 sub portsentry_start_cmd
136 {
137 return $config{'portsentry_start'} ? $config{'portsentry_start'} :
138         "$config{'portsentry'} -$config{'portsentry_tmode'} && $config{'portsentry'} -$config{'portsentry_umode'}";
139 }
140
141 # stop_portsentry()
142 # Stops portsentry
143 sub stop_portsentry
144 {
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);
148         }
149 else {
150         local @pids = &get_portsentry_pids();
151         if (@pids) {
152                 &kill_logged("TERM", @pids) ||
153                         return &text('portsentry_ekill', join(" ", @pids), $!);
154                 }
155         else {
156                 return $text{'portsentry_estopped'};
157                 }
158         }
159 return undef;
160 }
161
162 # start_portsentry()
163 # Starts portsentry, and returns an error message on failure, or undef 
164 sub start_portsentry
165 {
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);
169 return undef;
170 }
171
172 # list_hostsentry_modules($conf)
173 # Returns a list of all hostsentry python modules
174 sub list_hostsentry_modules
175 {
176 local $dir = &find_value("MODULE_PATH", $_[0]);
177 opendir(DIR, $dir);
178 local @rv = map { /^(\S+)\.py$/; $1 }
179                 grep { /\.py$/ && !/^moduleExample/ } readdir(DIR);
180 closedir(DIR);
181 return @rv;
182 }
183
184 # hostsentry_start_cmd()
185 sub hostsentry_start_cmd
186 {
187 return $config{'hostsentry_start'} ? $config{'hostsentry_start'}
188                                    : "python $config{'hostsentry'}";
189 }
190
191 # start_hostsentry()
192 # Start hostsentry, or return an error message
193 sub start_hostsentry
194 {
195 local $cmd = &hostsentry_start_cmd();
196 local $temp = &tempname();
197 &system_logged("$cmd >$temp 2>&1 </dev/null");
198 local $out;
199 open(TEMP, $temp);
200 while(<TEMP>) { $out .= $_; }
201 close(TEMP);
202 unlink($temp);
203 return "<tt>$out</tt>" if ($out =~ /failed|error/i);
204 return undef;
205 }
206
207 # stop_hostsentry()
208 # Stop hostsentry, or return an error message
209 sub stop_hostsentry
210 {
211 if ($config{'hostsentry_stop'}) {
212         local $out = &backquote_logged("($config{'hostsentry_stop'}) 2>&1 </dev/null")
213 ;
214         return "<tt>$out</tt>" if ($out =~ /error|failed/i);
215         }
216 else {
217         local $pid = &get_hostsentry_pid();
218         if ($pid) {
219                 &kill_logged("TERM", $pid) ||
220                         return &text('hostsentry_ekill', $pid, $!);
221                 }
222         else {
223                 return $text{'hostsentry_estopped'};
224                 }
225         }
226 return undef;
227 }
228
229 # get_hostsentry_pid()
230 sub get_hostsentry_pid
231 {
232 local ($pid) = grep { $_ != $$ } &find_byname("python.*hostsentry");
233 return $pid;
234 }
235
236 # get_hostsentry_dir()
237 sub get_hostsentry_dir
238 {
239 $config{'hostsentry_config'} =~ /^(\S+)\//;
240 return $1;
241 }
242
243 1;
244