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