new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / analysisd / compiled_rules / register_rule.sh
1 #!/bin/sh
2
3 # Variables - do not modify them
4 CHF="compiled_rules.h"
5
6 # Checking the location.
7 ls -la register_rule.sh > /dev/null 2>&1
8 if [ ! $? = 0 ]; then
9     LOCALDIR=`dirname $0`;
10     cd ${LOCALDIR}
11
12     ls -la register_rule.sh > /dev/null 2>&1
13     if [ ! $? = 0 ]; then
14         echo "ERROR: You must run this script from the same directory."
15         exit 1;
16     fi
17 fi
18
19 # Arguments
20 if [ "x$1" = "x" -o "x$1" = "xhelp" -o "x$1" = "x-h" ]; then
21     echo "$0 add <function_name>"
22     echo "$0 list"
23     echo "$0 build"
24     echo "$0 save"
25     echo "$0 restore"
26     exit 0;
27 fi
28
29 if [ "x$1" = "xlist" ]; then
30     echo "*Available functions: "
31     cat .function_list | sort | uniq;
32     exit 0;
33
34 elif [ "x$1" = "xsave" ]; then
35     ls -la /etc/ossec-init.conf > /dev/null 2>&1
36     if [ ! $? = 0 ]; then
37         echo "ERROR: Unable to save rules. You must have OSSEC installed to do so."
38         exit 1;
39     fi
40
41     cat /etc/ossec-init.conf > /dev/null 2>&1
42     if [ ! $? = 0 ]; then
43         echo "ERROR: Unable to save rules. You must be root to do so."
44         exit 1;
45     fi
46
47     . /etc/ossec-init.conf
48
49     ls ${DIRECTORY}/compiled_rules > /dev/null 2>&1
50     if [ ! $? = 0 ]; then
51         mkdir ${DIRECTORY}/compiled_rules > /dev/null 2>&1
52         if [ ! $? = 0 ]; then
53             echo "ERROR: Unable to save rules. You must be root to do so."
54             exit 1;
55         fi
56     fi
57
58     cp .function_list ${DIRECTORY}/compiled_rules/function_list > /dev/null 2>&1
59     if [ ! $? = 0 ]; then
60         echo "ERROR: Unable to save rules. You must be root to do so."
61         exit 1;
62     fi
63
64     for i in `ls *.c`; do
65         if [ ! "x$i" = "xgeneric_samples.c" ]; then
66             cp $i ${DIRECTORY}/compiled_rules/ > /dev/null 2>&1
67         fi
68     done
69     echo "*Save completed at ${DIRECTORY}/compiled_rules/";
70     exit 0;
71
72 elif [ "x$1" = "xrestore" ]; then
73
74     ls -la /etc/ossec-init.conf > /dev/null 2>&1
75     if [ ! $? = 0 ]; then
76         echo "ERROR: Unable to restore rules. You must have OSSEC installed to do so."
77         exit 1;
78     fi
79
80     cat /etc/ossec-init.conf > /dev/null 2>&1
81     if [ ! $? = 0 ]; then
82         echo "ERROR: Unable to restore rules. You must be root to do so."
83         exit 1;
84     fi
85
86     . /etc/ossec-init.conf
87
88     ls ${DIRECTORY}/compiled_rules/function_list > /dev/null 2>&1
89     if [ ! $? = 0 ]; then
90         echo "*No local compiled rules available to restore."
91         exit 0;
92     fi
93
94     cat  ${DIRECTORY}/compiled_rules/function_list >> .function_list
95     if [ ! $? = 0 ]; then
96         echo "ERROR: Unable to restore rules. Function list not present."
97         exit 1;
98     fi
99
100     for i in `ls ${DIRECTORY}/compiled_rules/*.c`; do
101         if [ ! "x$i" = "xgeneric_samples.c" ]; then
102             cp $i ./ > /dev/null 2>&1
103         fi
104     done
105     echo "*Restore completed from ${DIRECTORY}/compiled_rules/";
106     exit 0;
107
108 elif [ "x$1" = "xbuild" ]; then
109     ls -la .function_list > /dev/null 2>&1
110     if [ ! $? = 0 ]; then
111         echo "ERROR: Unable to build. No function is registered."
112         exit 1;
113     fi
114
115     # Auto generating the file.
116     echo "/* This file is auto generated by $0. Do not touch it. */" > ${CHF}
117     echo "" >> ${CHF};
118
119     echo "/* Adding the function definitions. */" >> ${CHF};
120     for i in `cat .function_list | sort| uniq`; do
121         echo "void *$i(Eventinfo *lf);" >> ${CHF};
122     done
123     echo "" >> ${CHF};
124
125     echo "/* Adding the rules list. */" >> ${CHF};
126     echo "void *(compiled_rules_list[]) = " >> ${CHF};
127     echo "{" >> ${CHF};
128     for i in `cat .function_list | sort| uniq`; do
129         echo "    $i," >> ${CHF};
130     done
131     echo "    NULL" >> ${CHF};
132     echo "};" >> ${CHF};
133     echo "" >> ${CHF};
134
135     echo "/* Adding the rules list names. */" >> ${CHF};
136     echo "const char *(compiled_rules_name[]) = " >> ${CHF};
137     echo "{" >> ${CHF};
138     for i in `cat .function_list |sort | uniq`; do
139         echo "    \"$i\"," >> ${CHF};
140     done
141     echo "    NULL" >> ${CHF};
142     echo "};" >> ${CHF};
143     echo "" >> ${CHF};
144     echo "/* EOF */" >> ${CHF};
145
146     echo "*Build completed."
147
148 elif [ "x$1" = "xadd" ]; then
149     if [ "x$2" = "x" ]; then
150         echo "ERROR: Missing function name.";
151         echo "ex: $0 add <function_name>";
152         exit 1;
153     fi
154
155     grep $2 ./*.c > /dev/null 2>&1
156     if [ ! $? = 0 ]; then
157         echo "ERROR: Function '$2' not found.";
158         exit 1;
159     fi
160
161     grep $2 .function_list > /dev/null 2>&1
162     if [ $? = 0 ]; then
163         echo "ERROR: Function '$2' already added.";
164         exit 1;
165     fi
166
167     echo $2 >> .function_list;
168     echo "*Function $2 added."
169
170 else
171     echo "ERROR: Invalid argument.";
172     exit 1;
173
174 fi
175