-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathldap_fsm.h
215 lines (184 loc) · 4.89 KB
/
ldap_fsm.h
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
* ldap_fsm.h
* LDAP server - ISA 2017/2018
* Author: Ondrej Kurak
* Mail: [email protected]
**/
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <iostream>
#include <vector>
#include <fstream>
#include <map>
#include <regex>
#include <set>
#include <algorithm>
/**
* Macro for debuging (make debug)
**/
#ifdef NDEBUG
#define DEBUG 1
#else
#define DEBUG 0
#endif
/**
* ProtokolOp macros
**/
#define BINDREQUEST 0x60
#define BINDRESPONSE 0x61
#define SEARCHREQEST 0x63
#define SEARCHRESULTENTRY 0x64
#define SEARCHRESULTDONE 0x65
#define UNBINDREQUEST 0x42
/**
* Filter macros
**/
#define AND 0xA0
#define OR 0xA1
#define NOT 0xA2
#define SUBSTRING 0xA4
#define EQUALITY 0xA3
using namespace std;
using namespace std::regex_constants;
/** LDAP Message
* Class for storing LDAP message
**/
class LDAP_message {
public:
int id; /**< Message ID **/
int length; /**< Length of message**/
int size_limit; /**< Maximal number of results (0=all) **/
int time_limit; /**< Maximal time for sending (0=none) **/
int type; /**< Type of LDAP message**/
int version; /**< Version of LDAP **/
};
/** LDAP Filter
* Class for storing LDAP filters in tree like structure
**/
class Filter {
public:
int type = -1; /**< Filter type **/
int length; /**< Length of filter (num of char) **/
vector<Filter> filters; /**< Stored subfilters **/
/**< Map for names of AttrDesc **/
map<string, int> known = {{"cn", 0}, {"commonname", 0},
{"uid", 1}, {"userid", 1},
{"mail", 2}};
string what; /**< AttrDesc **/
int w; /**< Index of AttrDesc **/
string value; /**< AttrValue **/
};
/** LDAP parser
* Class for parsing and generating LDAP messages
**/
class LDAP_parser {
public:
/** Constructor
* Initialization of LDAP_parser.
* @param newfd file descriptor
* @param d data with cn, uid, mail
**/
LDAP_parser(int newfd, set<vector<string>> d);
/** Start of parsing
* Starts parsing common part of LDAP messages
* @return true if successful
**/
bool start();
private:
int act; /**< Possition in message**/
int fd; /**< File descriptor **/
unsigned char ch; /**< Actual byte from message **/
set<vector<string>> data; /**< Data from input file**/
set<vector<string>> res_set; /**< Result of application of filters **/
Filter filter; /**< Stored filters**/
LDAP_message message; /**< Message informations**/
/** Next char of message
* Reads next char from message and inc act
**/
void next();
/** Clear parser
* Sets ch = 0 and atc = -1
**/
void clear();
/** BindRequest parsing
* Verification of BindReqest, sends BindResponse
* @return true if successful
**/
bool bind_req();
/** SearchRequest parsing
* Verification of SearchRequest and filtes.
* Resolve filtes and sends SearchResEntry
* for every result in res_set and SearchResDone.
* @return true if successful
**/
bool search_req();
/** UnBindRequest parsing
* Verification of UnBindRequest
* @return false ending connection
**/
bool unbind_req();
/** BindResponse generator/sender
* Generates and sends BindResponse
**/
void bind_response();
/** SearchResEntry generator/sender
* Generates and sends SearchResEntry for
* every result in res_set
**/
void search_res_entry();
/** SearchResDone generator/sender
* Generates and sends SearchResDone
**/
void search_res_done();
/** ber_functions.cc **/
/** Loads LL
* Loads length of message from actual ch
* @return length of message
**/
int get_ll();
/** Loads message ID
* Loads int from actual ch
* @return ID of message
*/
int get_int();
/** Loads string
* Loads string, starts by using make_ll
* to get length of string, then loads it
* @return loaded string
*/
string get_string();
/** Transformation from char to str
* Transforms unsigned char to str
* @param character
* @return string(1, ch)
*/
string cn(unsigned char ch);
/** Generating string with LL
* Generates string in LL+string form
* @param string
* @return LL+string
*/
string make_ll(string str);
/** Generating string from ID
* Generates string from ID
* @param ID
* @return string from ID
*/
string make_id(int num);
/**< filters.cc **/
/** Loading of filter
* Recursive loading filtes to tree like structure
* @return Tree like structure of filters
*/
Filter get_filter();
/** Printing filters
* Prints all filters
*/
void print_filters(Filter f);
/** Resolving filters
* Recursively resolves all filters in Filter
* @return entrys for filter
*/
set<vector<string>> resolve_filters(Filter f);
};