]>
Commit | Line | Data |
---|---|---|
ac37fd5e | 1 | #!/usr/bin/env bash |
a4bce147 VJ |
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 | ||
d1ac8393 JI |
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 | ||
a4bce147 VJ |
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" | |
a4bce147 | 67 | |
d1ac8393 JI |
68 | set_dir |
69 | ||
a4bce147 VJ |
70 | if [ ! -e detect-template.c ] || [ ! -e detect-template.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 | ||
452355bb VJ |
81 | FILE_C="tests/detect-${LC}.c" |
82 | if [ ! -e tests/detect-template.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" | |
a4bce147 VJ |
95 | cp detect-template.c $FILE_C |
96 | cp detect-template.h $FILE_H | |
97 | ||
98 | # search and replaces | |
99 | sed -i "s/TEMPLATE/${UC}/g" $FILE_C | |
100 | sed -i "s/TEMPLATE/${UC}/g" $FILE_H | |
101 | sed -i "s/Template/${NR}/g" $FILE_C | |
102 | sed -i "s/Template/${NR}/g" $FILE_H | |
103 | sed -i "s/template/${LC}/g" $FILE_C | |
104 | sed -i "s/template/${LC}/g" $FILE_H | |
105 | # add to Makefile.am | |
106 | sed -i "s/detect-template.c detect-template.h \\\/detect-template.c detect-template.h \\\\\n${FILE_C} ${FILE_H} \\\/g" Makefile.am | |
84f09d51 | 107 | |
a4bce147 | 108 | # update enum |
84f09d51 JI |
109 | sed -i "s/DETECT_TEMPLATE,/DETECT_TEMPLATE,\\n DETECT_${UC},/g" detect-engine-register.h |
110 | ||
111 | # add include to detect-engine-register.c | |
112 | sed -i "s/#include \"detect-template.h\"/#include \"detect-template.h\"\\n#include \"${FILE_H}\"/g" detect-engine-register.c | |
113 | ||
114 | # add reg func to detect-engine-register.c | |
115 | sed -i "s/DetectTemplateRegister();/DetectTemplateRegister();\\n Detect${NR}Register();/g" detect-engine-register.c | |
a4bce147 | 116 | |
452355bb VJ |
117 | # tests file |
118 | FILE_C="tests/detect-${LC}.c" | |
119 | cp tests/detect-template.c $FILE_C | |
120 | ||
121 | # search and replaces | |
122 | sed -i "s/TEMPLATE/${UC}/g" $FILE_C | |
123 | sed -i "s/Template/${NR}/g" $FILE_C | |
124 | sed -i "s/template/${LC}/g" $FILE_C | |
125 | ||
a4bce147 VJ |
126 | Done |
127 | exit 0 |