39bf193835091d03369b0fc13655a2b25c5677b5
[ossec-hids.git] / debian / ossec-hids / usr / share / doc / ossec-hids / contrib / ossec-pcre2-config.pl
1 #! /usr/bin/perl -w
2
3 use strict;
4 use warnings;
5
6 use Cwd qw/getcwd realpath/;
7 use File::Basename;
8 use File::Find;
9 use File::Temp qw/tempfile/;
10
11 my $ossec_regex_convert = realpath(dirname($0) . '/../src/ossec-regex-convert');
12
13 sub get_install_dir () {
14     open(FILE, '<', 'src/LOCATION') || die("Cannot find INSTALL DIR");
15     my $dir = '/var/ossec';
16
17     while (<FILE>) {
18         if (m{^DIR\s*=\s*(["']?)(.*)\g1$}p) {
19             $dir = $2;
20             last;
21         }
22     }
23
24     return $dir;
25 }
26
27 my $old_tags = join('|', split(/\n/m, `$ossec_regex_convert -t`));
28
29 sub convert_file ($) {
30     my $filename = shift();
31     print("Converting ${filename}...\n");
32
33     unless (open(SRC, '<', $filename)) {
34         print(STDERR "Cannot read '${filename}'\n");
35         return;
36     }
37     my ($tmp_fh, $tmp_filename) = tempfile('tmp-ossec-config-convert.XXXXX', DIR => '/tmp', SUFFIX => '.xml');
38
39     while (<SRC>) {
40         if (m{^(\s*)<\s*($old_tags)([^>]*)>(.*?)<\s*/\s*\g2\s*>}pg) {
41             my ($indent, $old_type, $options, $old_regex) = ($1, $2, $3, $4);
42             $old_regex =~ s/'/'\\''/g;
43             my $out = qx/$ossec_regex_convert -b -- $old_type '$old_regex'/;
44             chomp($out);
45             my ($type, $regex) = split(/ /, $out, 2);
46             if ($old_regex) {
47                 print($tmp_fh "$indent<$type$options>$regex</$type>\n");
48             } else {
49                 print($tmp_fh "$indent<$type$options></$type>\n");
50             }
51         } else {
52             print($tmp_fh $_);
53         }
54     }
55
56     close(SRC);
57     close($tmp_fh);
58
59     rename($tmp_filename, $filename);
60 }
61
62 sub wanted() {
63     my $filename = $File::Find::name;
64
65     if ($filename =~ m/[.]xml$/) {
66         convert_file($filename);
67     }
68 }
69
70 my $INSTALL_DIR = get_install_dir();
71 if (! -d ${INSTALL_DIR}) {
72     print(STDERR "Please install OSSEC first\n");
73     exit(1);
74 }
75
76 find({wanted => \&wanted, no_chdir => 1}, $INSTALL_DIR);
77
78 exit(0);