aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/suricata/scripts/setup-app-layer.sh
blob: b24b5e6195d861304701716108f5be7fe994fa73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#! /bin/sh
#
# Script to provision a new application layer detector and parser.

set -e
#set -x

function usage() {
    cat <<EOF

usage: $0 <protocol name>

This script will provision a new app-layer parser for the protocol
name specified on the command line. This is done by copying and
patching src/app-layer-template.[ch] then linking the new files into
the build system.

Examples:

    $0 DNP3
    $0 Gopher

EOF
}

fail_if_exists() {
    path="$1"
    if test -e "${path}"; then
	echo "error: ${path} already exists."
	exit 1
    fi
}

function copy_template_file() {
    src="$1"
    dst="$2"

    echo "Creating ${dst}."
    
    sed -e "s/TEMPLATE/${protoname_upper}/g" \
	-e "s/template/${protoname_lower}/g" \
	-e "s/Template/${protoname}/g" > ${dst} < ${src}
}

function copy_app_layer_templates {
    src_h="src/app-layer-template.h"
    dst_h="src/app-layer-${protoname_lower}.h"
    src_c="src/app-layer-template.c"
    dst_c="src/app-layer-${protoname_lower}.c"

    fail_if_exists ${dst_h}
    fail_if_exists ${dst_c}

    copy_template_file ${src_h} ${dst_h}
    copy_template_file ${src_c} ${dst_c}
}

function patch_makefile_am {
    filename="src/Makefile.am"
    echo "Patching ${filename}."
    ed -s ${filename} > /dev/null <<EOF
/app-layer-template
t-
s/template/${protoname_lower}/g
w
EOF
}

function patch_app_layer_protos_h {
    filename="src/app-layer-protos.h"
    echo "Patching ${filename}."
    ed -s ${filename} > /dev/null <<EOF
/ALPROTO_TEMPLATE
t-
s/TEMPLATE/${protoname_upper}/
w
EOF
}

function patch_app_layer_protos_c {
    filename="src/app-layer-protos.c"
    echo "Patching ${filename}."
    ed -s ${filename} > /dev/null <<EOF
/case ALPROTO_TEMPLATE
.,+2t-
-2
s/TEMPLATE/${protoname_upper}/
+
s/template/${protoname_lower}/
w
EOF
}

function patch_app_layer_detect_proto_c() {
    filename="src/app-layer-detect-proto.c"
    echo "Patching ${filename}."
    ed -s ${filename} > /dev/null <<EOF
/== ALPROTO_TEMPLATE
.,+t-
-,.s/TEMPLATE/${protoname_upper}/
+3
/== ALPROTO_TEMPLATE
.,+t-
-,.s/TEMPLATE/${protoname_upper}/
+3
w
EOF
}

function patch_app_layer_parser_c() {
    filename="src/app-layer-parser.c"
    echo "Patching ${filename}."
    ed -s ${filename} > /dev/null <<EOF
/#include "app-layer-template.h"
t-
s/template/${protoname_lower}/
/RegisterTemplateParsers
t-
s/Template/${protoname}/
w
EOF
}

function patch_suricata_yaml_in() {
    filename="suricata.yaml.in"
    echo "Patching ${filename}."
    ed -s ${filename} > /dev/null <<EOF
/^app-layer:
/protocols:
a
    ${protoname_lower}:
      enabled: yes
.
w
EOF
}

protoname="$1"

if [ "${protoname}" = "" ]; then
    usage
    exit 1
fi

protoname_lower=$(printf ${protoname} | tr '[:upper:]' '[:lower:]')
protoname_upper=$(printf ${protoname} | tr '[:lower:]' '[:upper:]')

copy_app_layer_templates
patch_makefile_am
patch_app_layer_protos_h
patch_app_layer_protos_c
patch_app_layer_detect_proto_c
patch_app_layer_parser_c
patch_suricata_yaml_in

cat <<EOF

An application detector and parser for the protocol ${protoname} has
now been setup in the files:

    src/app-layer-${protoname_lower}.h
    src/app-layer-${protoname_lower}.c

and should now build cleanly. Try running 'make'.

EOF