]> git.ipfire.org Git - people/ms/suricata.git/blame - scripts/setup-simple-detect.sh
tools: bash from env
[people/ms/suricata.git] / scripts / setup-simple-detect.sh
CommitLineData
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
7set -e
8#set -x
9
10function 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
23function 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.
36set_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
48if [ $# -ne "1" ]; then
49 Usage
50 echo "ERROR: call with one argument"
51 exit 1
52fi
53
54INPUT=$1
55# lowercase
56LC=${INPUT,,}
57#echo $LC
58# UPPERCASE
59UC=${LC^^}
60#echo $UC
61# Normal
62NR=${LC^}
63#echo $NR
64
65FILE_C="detect-${LC}.c"
66FILE_H="detect-${LC}.h"
a4bce147 67
d1ac8393
JI
68set_dir
69
a4bce147
VJ
70if [ ! -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
74fi
75if [ -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
79fi
80
452355bb
VJ
81FILE_C="tests/detect-${LC}.c"
82if [ ! -e tests/detect-template.c ]; then
83 Usage
84 echo "ERROR: input file tests/detect-template.c is missing"
85 exit 1
86fi
87if [ -e $FILE_C ]; then
88 Usage
89 echo "ERROR: file $FILE_C already exist, won't overwrite"
90 exit 1
91fi
92
93FILE_C="detect-${LC}.c"
94FILE_H="detect-${LC}.h"
a4bce147
VJ
95cp detect-template.c $FILE_C
96cp detect-template.h $FILE_H
97
98# search and replaces
99sed -i "s/TEMPLATE/${UC}/g" $FILE_C
100sed -i "s/TEMPLATE/${UC}/g" $FILE_H
101sed -i "s/Template/${NR}/g" $FILE_C
102sed -i "s/Template/${NR}/g" $FILE_H
103sed -i "s/template/${LC}/g" $FILE_C
104sed -i "s/template/${LC}/g" $FILE_H
105# add to Makefile.am
106sed -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
109sed -i "s/DETECT_TEMPLATE,/DETECT_TEMPLATE,\\n DETECT_${UC},/g" detect-engine-register.h
110
111# add include to detect-engine-register.c
112sed -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
115sed -i "s/DetectTemplateRegister();/DetectTemplateRegister();\\n Detect${NR}Register();/g" detect-engine-register.c
a4bce147 116
452355bb
VJ
117# tests file
118FILE_C="tests/detect-${LC}.c"
119cp tests/detect-template.c $FILE_C
120
121# search and replaces
122sed -i "s/TEMPLATE/${UC}/g" $FILE_C
123sed -i "s/Template/${NR}/g" $FILE_C
124sed -i "s/template/${LC}/g" $FILE_C
125
a4bce147
VJ
126Done
127exit 0