-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconftree.c
251 lines (213 loc) · 6.44 KB
/
conftree.c
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/* conftree.c
* Copyright (C) 2006-2007 Tillmann Werner <[email protected]>
*
* This file is free software; as a special exception the author gives
* unlimited permission to copy and/or distribute it, with or without
* modifications, as long as this notice is preserved.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <errno.h>
#include <stdio.h>
#include <pwd.h>
#include <grp.h>
#include <unistd.h>
#include <netdb.h>
#include <strings.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/file.h>
#include "honeytrap.h"
#include "logging.h"
#include "conftree.h"
void conftree_children_free(conf_node *tree) {
conf_node *cur_node, *old_node;
list_entry *val, *old_val;
if (!tree) return;
cur_node = tree->first_leaf;
while (cur_node) {
if (cur_node->first_leaf) conftree_children_free(cur_node->first_leaf);
free(cur_node->keyword);
val = cur_node->val;
while (val) {
free(val->data);
old_val = val;
val = val->next;
free(old_val);
}
old_node = cur_node;
cur_node = cur_node->next;
free(old_node);
}
return;
}
void print_conftree(conf_node *tree, int depth) {
int i;
char c;
conf_node *cur_node;
list_entry *val;
cur_node = tree;
while(cur_node) {
if (cur_node->keyword) {
for (i=0; i<depth*2; i++) printf(" ");
printf("%s", cur_node->keyword);
val = cur_node->val;
while (val) {
if (val->data) {
printf("\n\t\t\"");
for( i = 0; i < val->size; i++ )
printf("%c", isprint(c = *((const char *)(val->data+i))) ? c : '.');
printf("\"");
}
val = val->next;
}
printf("\n");
}
print_conftree(cur_node->first_leaf, depth+1);
cur_node = cur_node->next;
}
return;
}
list_entry *add_list_item(conf_node *node, const void *data, ssize_t size) {
list_entry *new_entry, *cur_entry;
if (!node) return(NULL);
/* create new element */
if ((new_entry = malloc(sizeof(list_entry))) == NULL) {
logmsg(LOG_ERR, 1, "Error - Unable to allocate memory: %m.\n");
return(NULL);
}
memset(new_entry, 0, sizeof(list_entry));
/* copy data */
if ((new_entry->data = malloc(size+1)) == NULL) {
logmsg(LOG_ERR, 1, "Error - Unable to allocate memory: %m.\n");
return(NULL);
}
memset(new_entry->data, 0, size+1);
new_entry->size = size;
memcpy(new_entry->data, data, size);
/* attach new element to list tail and return */
if ((cur_entry = node->val) != NULL) {
while (cur_entry->next) cur_entry = cur_entry->next;
cur_entry->next = new_entry;
} else node->val = new_entry;
return(new_entry);
}
/* return first leaf node of subtree for given keyword */
conf_node *conf_subtree(conf_node *tree, const char *keyword) {
conf_node *subtree;
if ((subtree = check_keyword(tree, keyword)) == NULL) return(NULL);
return(subtree->first_leaf);
}
/* check whether (the prefix of) a keyword does exist in the tree *
* and return a pointer to the node containing its last part */
conf_node *check_keyword(conf_node *tree, const char *keyword) {
conf_node *cur_node;
char **key, *subkey;
cur_node = tree;
subkey = NULL;
if (!tree) return(NULL);
if (!keyword) {
fprintf(stderr, " Error - Unable to search tree: No keyword given.\n");
return(NULL);
}
if (((key = (char **) malloc(sizeof(char *))) == NULL) || ((*key = strdup(keyword)) == NULL)) {
fprintf(stderr, " Error - Unable to allocate memory: %m.\n");
return(NULL);
}
/* search in config tree */
if ((subkey = strsep(key, ".")) == NULL) {
free(key);
return(NULL);
}
while (cur_node) {
/* compare current node's keyword with prefix */
if (strcmp(cur_node->keyword, subkey) == 0) {
if ((subkey = strsep(key, ".")) == NULL) return(cur_node);
cur_node = cur_node->first_leaf;
} else cur_node = cur_node->next;
}
free(key);
return(NULL);
}
/* insert new node into config tree and return a pointer to it
* if *tree is NULL, it will be set to point to the root node */
conf_node *add_keyword(conf_node **tree, const char *keyword, const void *data, ssize_t size) {
conf_node *new_node, *cur_node;
char *key, *subkey;
cur_node = *tree;
new_node = NULL;
key = NULL;
subkey = NULL;
if (!keyword) {
logmsg(LOG_WARN, 1, "Warning - Unable to extend configuration tree: No keyword given.\n");
return(*tree);
}
// check whether a prefix does already exist and if not, add it recursively
if ((key = strdup(keyword)) == NULL) {
logmsg(LOG_ERR, 1, "Error - Unable to allocate memory: %m.\n");
return(NULL);
}
/* add recursively */
if ((cur_node = check_keyword(*tree, key)) == NULL) {
if ((subkey = strrchr(key, '.')) != NULL) {
subkey[0] = 0; // zero-terminate first half
subkey++; // pointer to second half
if (isdigit(subkey[0])) {
if ((cur_node = check_keyword(*tree, key)) == NULL)
if ((cur_node = add_keyword(tree, key, NULL, 0)) == NULL) return(NULL);
if (add_list_item(cur_node, data, size) == NULL) {
fprintf(stderr, " Error - Unable to add list item for %s.\n", key);
return(NULL);
}
return(cur_node);
}
if ((cur_node = add_keyword(tree, key, NULL, 0)) == NULL) return(NULL);
}
} else return(cur_node);
// create new node and insert it into tree
if ((new_node = malloc(sizeof(conf_node))) == NULL) {
logmsg(LOG_ERR, 1, "Error - Unable to allocate memory: %m.\n");
free(key);
return(NULL);
}
memset(new_node, 0, sizeof(conf_node));
// if keyword is a toplevel key, add it, else add subkey
if ((new_node->keyword = strdup(subkey ? subkey : key)) == NULL) {
logmsg(LOG_ERR, 1, "Error - Unable to allocate memory: %m.\n");
free(key);
return(NULL);
}
free(key);
if (size) {
if (add_list_item(new_node, data, size) == NULL) {
fprintf(stderr, " Error - Unable to add list item for %s.\n", keyword);
return(NULL);
}
}
/* insert new node into tree */
if (cur_node) {
/* it's an internal node */
if (cur_node->first_leaf) {
cur_node = cur_node->first_leaf;
while (cur_node->next) cur_node = cur_node->next;
cur_node->next = new_node;
} else cur_node->first_leaf = new_node;
} else {
/* it's a top level node */
if (!(*tree)) (*tree) = new_node;
/* it's a root's neighbor */
else {
cur_node = *tree;
while (cur_node->next) cur_node = cur_node->next;
cur_node->next = new_node;
}
}
if (*tree == NULL) *tree = cur_node;
return(new_node);
}