1 /*
2  * ModSecurity, http://www.modsecurity.org/
3  * Copyright (c) 2015 - 2021 Trustwave Holdings, Inc. (http://www.trustwave.com/)
4  *
5  * You may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * If any of the files related to licensing are missing or if you have any
11  * other questions related to licensing please contact Trustwave Holdings, Inc.
12  * directly using the email address security@modsecurity.org.
13  *
14  */
15 
16 #ifdef __cplusplus
17 #include <string>
18 #include <map>
19 #include <stack>
20 #include <vector>
21 #include <list>
22 #endif
23 
24 
25 #ifndef SRC_PARSER_DRIVER_H_
26 #define SRC_PARSER_DRIVER_H_
27 
28 #include "modsecurity/modsecurity.h"
29 #include "modsecurity/rules_set.h"
30 #include "modsecurity/rules_set_properties.h"
31 #include "modsecurity/audit_log.h"
32 #include "src/rule_script.h"
33 #ifndef MS_CPPCHECK_DISABLED_FOR_PARSER
34 #include "src/parser/seclang-parser.hh"
35 #endif
36 
37 using modsecurity::RuleWithOperator;
38 using modsecurity::RulesSet;
39 
40 
41 # define YY_DECL \
42   yy::seclang_parser::symbol_type yylex(modsecurity::Parser::Driver& driver)
43 
44 YY_DECL;
45 
46 namespace modsecurity {
47 namespace Parser {
48 
49 #ifdef __cplusplus
50 class Driver;
51 #else
52 typedef struct Driver_t Driver;
53 #endif
54 
55 
56 /**
57  *
58  * FIXME: There is a memory leak in the filename at yy::location.
59  *        The filename should be converted into a shared string to
60  *        save memory or be associated with the life cycle of the
61  *        driver class.
62  *
63  **/
64 class Driver : public RulesSetProperties {
65  public:
66     Driver();
67     virtual ~Driver();
68 
69     int addSecRule(std::unique_ptr<RuleWithActions> rule);
70     int addSecAction(std::unique_ptr<RuleWithActions> rule);
71     int addSecMarker(std::string marker, std::unique_ptr<std::string> fileName, int lineNumber);
72     int addSecRuleScript(std::unique_ptr<RuleScript> rule);
73 
74     bool scan_begin();
75     void scan_end();
76     bool trace_scanning;
77 
78     int parseFile(const std::string& f);
79     int parse(const std::string& f, const std::string &ref);
80 
81     std::string file;
82 
83     bool trace_parsing;
84 
85     void error(const yy::location& l, const std::string& m);
86     void error(const yy::location& l, const std::string& m,
87         const std::string& c);
88 
89     std::list<yy::location *> loc;
90 
91     std::string buffer;
92     RuleWithActions *m_lastRule;
93 
94     RulesSetPhases m_rulesSetPhases;
95 };
96 
97 
98 }  // namespace Parser
99 }  // namespace modsecurity
100 
101 #endif  // SRC_PARSER_DRIVER_H_
102