]> git.ipfire.org Git - people/ms/suricata.git/blob - scripts/setup-simple-detect2.sh
tools: bash from env
[people/ms/suricata.git] / scripts / setup-simple-detect2.sh
1 #!/usr/bin/env bash
2 #
3 # Script to setup a new 'simple' detect module.
4 # Written by Victor Julien <victor@inliniac.net>
5 #
6
7 set -e
8 #set -x
9
10 function Usage {
11 echo
12 echo "$(basename $0) -- script to provision a detect module. The script"
13 echo "makes a copy of detect-template, sets the name and updates"
14 echo "the build system."
15 echo
16 echo "Call from the 'src' directory, with one argument: the detect module"
17 echo "name."
18 echo
19 echo "E.g. inside 'src': ../scripts/$(basename $0) helloworld"
20 echo
21 }
22
23 function Done {
24 echo
25 echo "Detect module $NR has been set up in $FILE_C and $FILE_H"
26 echo "and the build system has been updated."
27 echo
28 echo "The detect module should now compile cleanly. Try running 'make'."
29 echo
30 echo "Next steps are to edit the files to implement the actual"
31 echo "detection logic of $NR."
32 echo
33 }
34
35 # Make sure we are running from the correct directory.
36 set_dir() {
37 if [ -e ./suricata.c ]; then
38 # Do nothing.
39 true
40 elif [ -e ./src/suricata.c ]; then
41 cd src
42 else
43 echo "error: this does not appear to be a suricata source directory."
44 exit 1
45 fi
46 }
47
48 if [ $# -ne "1" ]; then
49 Usage
50 echo "ERROR: call with one argument"
51 exit 1
52 fi
53
54 INPUT=$1
55 # lowercase
56 LC=${INPUT,,}
57 #echo $LC
58 # UPPERCASE
59 UC=${LC^^}
60 #echo $UC
61 # Normal
62 NR=${LC^}
63 #echo $NR
64
65 FILE_C="detect-${LC}.c"
66 FILE_H="detect-${LC}.h"
67
68 set_dir
69
70 if [ ! -e detect-template2.c ] || [ ! -e detect-template2.h ]; then
71 Usage
72 echo "ERROR: input files detect-template.c and/or detect-template.h are missing"
73 exit 1
74 fi
75 if [ -e $FILE_C ] || [ -e $FILE_H ]; then
76 Usage
77 echo "ERROR: file(s) $FILE_C and/or $FILE_H already exist, won't overwrite"
78 exit 1
79 fi
80
81 FILE_C="tests/detect-${LC}.c"
82 if [ ! -e tests/detect-template2.c ]; then
83 Usage
84 echo "ERROR: input file tests/detect-template.c is missing"
85 exit 1
86 fi
87 if [ -e $FILE_C ]; then
88 Usage
89 echo "ERROR: file $FILE_C already exist, won't overwrite"
90 exit 1
91 fi
92
93 FILE_C="detect-${LC}.c"
94 FILE_H="detect-${LC}.h"
95 cp detect-template2.c $FILE_C
96 cp detect-template2.h $FILE_H
97
98 # search and replaces
99 sed -i "s/TEMPLATE2/${UC}/g" $FILE_C
100 sed -i "s/TEMPLATE2/${UC}/g" $FILE_H
101 sed -i "s/Template2/${NR}/g" $FILE_C
102 sed -i "s/Template2/${NR}/g" $FILE_H
103 sed -i "s/template2/${LC}/g" $FILE_C
104 sed -i "s/template2/${LC}/g" $FILE_H
105 # add to Makefile.am
106 sed -i "s/detect-template2.c detect-template2.h \\\/detect-template2.c detect-template2.h \\\\\n${FILE_C} ${FILE_H} \\\/g" Makefile.am
107
108 # update enum
109 sed -i "s/DETECT_TEMPLATE2,/DETECT_TEMPLATE2,\\n DETECT_${UC},/g" detect-engine-register.h
110
111 # add include to detect-engine-register.c
112 sed -i "s/#include \"detect-template2.h\"/#include \"detect-template2.h\"\\n#include \"${FILE_H}\"/g" detect-engine-register.c
113
114 # add reg func to detect-engine-register.c
115 sed -i "s/DetectTemplate2Register();/DetectTemplate2Register();\\n Detect${NR}Register();/g" detect-engine-register.c
116
117 # tests file
118 FILE_C="tests/detect-${LC}.c"
119 cp tests/detect-template2.c $FILE_C
120
121 # search and replaces
122 sed -i "s/TEMPLATE2/${UC}/g" $FILE_C
123 sed -i "s/Template2/${NR}/g" $FILE_C
124 sed -i "s/template2/${LC}/g" $FILE_C
125
126 Done
127 exit 0