1 /*
2  * AIDE (Advanced Intrusion Detection Environment)
3  *
4  * Copyright (C) 2019-2021 Hannes von Haugwitz
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 
21 #ifndef _CONF_AST_H_INCLUDED
22 #define  _CONF_AST_H_INCLUDED
23 
24 #include <stdbool.h>
25 #include "rx_rule.h"
26 
27 typedef enum config_option {
28     ACL_NO_SYMLINK_FOLLOW_OPTION,
29     DATABASE_ADD_METADATA_OPTION,
30     DATABASE_ATTRIBUTES_OPTION,
31     DATABASE_GZIP_OPTION,
32     DATABASE_IN_OPTION,
33     DATABASE_OUT_OPTION,
34     DATABASE_NEW_OPTION,
35     LOG_LEVEL_OPTION,
36     REPORT_BASE16_OPTION,
37     REPORT_DETAILED_INIT_OPTION,
38     REPORT_FORCE_ATTRS_OPTION,
39     REPORT_GROUPED_OPTION,
40     REPORT_IGNORE_ADDED_ATTRS_OPTION,
41     REPORT_IGNORE_REMOVED_ATTRS_OPTION,
42     REPORT_IGNORE_CHANGED_ATTRS_OPTION,
43     REPORT_IGNORE_E2FSATTRS_OPTION,
44     REPORT_LEVEL_OPTION,
45     REPORT_QUIET_OPTION,
46     REPORT_APPEND_OPTION,
47     REPORT_SUMMARIZE_CHANGES_OPTION,
48     REPORT_URL_OPTION,
49     ROOT_PREFIX_OPTION,
50     WARN_DEAD_SYMLINKS_OPTION,
51     VERBOSE_OPTION,
52     CONFIG_VERSION,
53 } config_option;
54 
55 typedef enum attribute_operator {
56         ATTR_OP_PLUS = 0,
57         ATTR_OP_MINUS,
58         ATTR_OP_GROUP,
59 } attribute_operator;
60 
61 typedef struct attribute_expression {
62     attribute_operator op;
63 
64     struct attribute_expression* left;
65     char* right;
66 } attribute_expression;
67 
68 typedef enum string_operator {
69         STR_OP_STR,
70         STR_OP_VARIABLE,
71         STR_OP_CONCAT,
72 } string_operator;
73 typedef struct string_expression {
74     string_operator op;
75 
76     char* str;
77     struct string_expression* left;
78     struct string_expression* right;
79 } string_expression;
80 
81 typedef struct config_option_statement {
82     config_option option;
83     attribute_expression *a;
84     string_expression* e;
85 } config_option_statement;
86 
87 typedef enum bool_operator {
88         BOOL_OP_NOT,
89         BOOL_OP_DEFINED,
90         BOOL_OP_HOSTNAME,
91 } bool_operator;
92 
93 typedef struct bool_expression {
94     bool_operator op;
95 
96     string_expression* expr;
97     struct bool_expression* left;
98     struct bool_expression* right;
99 } bool_expression;
100 
101 typedef struct if_condition {
102     bool_expression* expression;
103 
104     int linenumber;
105     char *filename;
106     char* linebuf;
107 } if_condition;
108 
109 typedef struct if_statement {
110     struct if_condition* condition;
111 
112     struct ast* if_branch;
113     struct ast* else_branch;
114 } if_statement;
115 
116 typedef struct define_statement {
117     char *name;
118     string_expression *value;
119 } define_statement;
120 
121 typedef struct include_statement {
122     string_expression *path;
123     string_expression *rx;
124     bool execute;
125 } include_statement;
126 
127 typedef struct x_include_setenv_statement {
128     char *variable;
129     string_expression *value;
130 } x_include_setenv_statement;
131 
132 typedef struct undefine_statement {
133     char *name;
134 } undefine_statement;
135 
136 typedef struct group_statement {
137     char *name;
138     attribute_expression *expr;
139 } group_statement;
140 
141 typedef struct restriction_expression {
142     char* right;
143     struct restriction_expression* left;
144 } restriction_expression;
145 
146 typedef struct rule_statement {
147     AIDE_RULE_TYPE type;
148 
149     string_expression *path;
150     restriction_expression *restriction;
151     attribute_expression *attributes;
152 } rule_statement;
153 
154 typedef struct ast {
155     enum {
156         config_option_type,
157 
158         include_statement_type,
159         x_include_setenv_statement_type,
160         define_statement_type,
161         undefine_statement_type,
162 
163         group_statement_type,
164 
165         if_statement_type,
166 
167         rule_statement_type,
168     } type;
169 
170     union {
171         config_option_statement _config;
172         include_statement _include;
173         x_include_setenv_statement _x_include_setenv;
174         define_statement _define;
175         undefine_statement _undefine;
176         group_statement _group;
177         if_statement _if;
178         rule_statement _rule;
179     } statement;
180 
181     int linenumber;
182     char *filename;
183     char* linebuf;
184 
185     struct ast* next;
186 } ast;
187 
188 string_expression* new_string(char*);
189 string_expression* new_variable(char*);
190 string_expression* new_string_concat(string_expression*, string_expression*);
191 
192 ast* new_string_option_statement(config_option, string_expression*);
193 ast* new_attribute_option_statement(config_option, attribute_expression*);
194 
195 ast* new_define_statement(char*, string_expression*);
196 ast* new_undefine_statement(char*);
197 
198 ast* new_group_statement(char*, attribute_expression*);
199 
200 ast* new_include_statement(string_expression*, string_expression*, bool);
201 ast* new_x_include_setenv_statement(char*, string_expression*);
202 
203 ast* new_if_statement(struct if_condition*, struct ast*, struct ast*);
204 
205 ast* new_rule_statement(AIDE_RULE_TYPE, string_expression*, restriction_expression*, attribute_expression*);
206 
207 if_condition* new_if_condition(struct bool_expression*);
208 
209 bool_expression* new_string_bool_expression(bool_operator, string_expression*);
210 bool_expression* new_bool_expression(bool_operator, bool_expression*, bool_expression*);
211 
212 attribute_expression* new_attribute_expression(attribute_operator, attribute_expression*, char*);
213 restriction_expression* new_restriction_expression(restriction_expression*, char*);
214 
215 void deep_free(ast*);
216 
217 #endif
218