Skip to content

Commit 8a080ca

Browse files
committed
Tweak some of the code and add --valid-dtd option
1 parent 187a09c commit 8a080ca

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

secef.fcgi

+15-13
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ _CONTENT_TYPES = (b'text/xml', b'application/text')
2121

2222
client = None
2323
logger = logging.getLogger()
24+
gateway = None
25+
2426

2527
class DTDResolver(etree.Resolver):
2628
def __init__(self, *args, **kwargs):
@@ -34,15 +36,12 @@ class DTDResolver(etree.Resolver):
3436

3537

3638
class Gateway(object):
37-
_parser = None
38-
39-
def __init__(self):
40-
if Gateway._parser is None:
41-
parser = etree.XMLParser(load_dtd=False)
42-
parser.resolvers.add(DTDResolver())
43-
Gateway._parser = parser
39+
def __init__(self, validate_dtd=False):
40+
parser = etree.XMLParser(load_dtd=validate_dtd)
41+
parser.resolvers.add(DTDResolver())
42+
self._parser = parser
4443

45-
def convert(self, body):
44+
def forward(self, body):
4645
roots = ('Alert', 'Heartbeat')
4746
ad_types = ('boolean', 'byte', 'character', 'date-time', 'integer',
4847
'ntpstamp', 'portlist', 'real', 'string', 'byte-string',
@@ -55,7 +54,7 @@ class Gateway(object):
5554
special_content.update(dict.fromkeys(ad_types, 'data'))
5655
ignored_attrs = ('ntpstamp', )
5756

58-
xml = etree.fromstring(body, parser=Gateway._parser)
57+
xml = etree.fromstring(body, parser=self._parser)
5958
stack = []
6059
indices = []
6160
idmef = None
@@ -148,13 +147,15 @@ class Gateway(object):
148147

149148

150149
def app(environ, start_response):
150+
global gateway
151+
151152
if environ.get('CONTENT_TYPE') not in _CONTENT_TYPES:
152153
start_response(b'415 Unsupported Media Type', [(b'Content-Type', b'text/plain')])
153154
return(b'Wrong media type\n')
154155

155156
try:
156157
remote = cgi.FieldStorage(environ['wsgi.input'], environ=environ)
157-
Gateway().convert(remote.value)
158+
gateway.forward(remote.value)
158159
start_response(b'200 OK', [(b'Content-Type', b'text/plain')])
159160
return(b'OK\n')
160161
except Exception as e:
@@ -165,13 +166,14 @@ def app(environ, start_response):
165166

166167
if __name__ == "__main__":
167168
parser = argparse.ArgumentParser(description="IDMEF to Prelude web gateway")
169+
parser.add_argument('--debug', help="Enable debugging logs.", default=False, action='store_true')
170+
parser.add_argument('--dry-run', '-n', help="Do not actually forward the messages.", dest='dry_run', action='store_true')
168171
parser.add_argument('--profile', help="Prelude profile to use.", default="secef")
169172
parser.add_argument('--sock', '-s', help="Path to the gateway's UNIX socket.", default=FCGI_SOCK)
170-
parser.add_argument('--debug', help="Enable debugging logs.", default=False, action='store_true')
171-
parser.add_argument('--dry-run', '-n', help="Do not actually forward the messages.",
172-
dest='dry_run', action='store_true')
173+
parser.add_argument('--valid-dtd', help="Perform DTD validation.", dest='valid_dtd', action='store_true')
173174
args = parser.parse_args()
174175
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG if args.debug else logging.INFO)
176+
gateway = Gateway(validate_dtd=bool(args.valid_dtd))
175177

176178
if not args.dry_run:
177179
client = prelude.ClientEasy(args.profile, prelude.ClientEasy.PERMISSION_IDMEF_WRITE,

0 commit comments

Comments
 (0)