-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharp.cpp
326 lines (270 loc) · 7.5 KB
/
arp.cpp
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/***************************************************************************
* Copyright (C) 2006 by Dmitriy Gorbenko *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include "sys/wait.h"
#include "define.h"
#include "str.h"
#include "message.h"
#include "config.h"
#include "tools.h"
#include "arp.h"
using namespace std;
extern char * return_message;
extern struct config_data_def config_data;
unsigned int arp_manage(char *cmd)
{
unsigned int result = FALSE;
char * act = NULL;
act = get_word_by_number(cmd, 2);
if (!act)
return FALSE;
if (strcmp(act, "UPDATE") == 0) {
if (update_arp(cmd) == FALSE) {
result = FALSE;
goto arp_end;
}
result = TRUE;
}
if (strcmp(act, "DELETE") == 0) {
if (delete_arp(cmd) == FALSE) {
result = FALSE;
goto arp_end;
}
result = TRUE;
}
arp_end:
if (act) safe_free(&act);
return result;
}
unsigned int update_arp(char * cmd)
{
unsigned int result = FALSE;
unsigned int file_updated = FALSE;
int fork_res;
char * buff = NULL;
char * tmp = NULL;
char * unique = NULL;
char * ip = NULL;
char * mac = NULL;
char * network = NULL;
unsigned int k =0, c = 0;
FILE * arp_file = NULL;
FILE * tmpf = NULL;
char * arp_file_name = NULL;
char * tmp_file_name = NULL;
ip = get_word_by_number(cmd, 3);
if (!ip) {
result = FALSE;
goto update_arp_end;
}
if (check_item_exists(config_data.denied_for, ip) == TRUE) {
return_message = strdup("IP ADDRESS IS NOT ALLOWED");
message("IP ADDRESS IS NOT ALLOWED: %s\n", ip);
result = FALSE;
goto update_arp_end;
};
mac = get_word_by_number(cmd, 4);
if (!mac) {
result = FALSE;
goto update_arp_end;
}
k = 0; c = 0;
while (strlen(ip) > k && c != 2) {
if (ip[k] == '.')
c++;
k++;
}
network = add_char_to_string(network, ip[k++]);
while (ip[k] && ip[k] != '.') {
network = add_char_to_string(network, ip[k++]);
}
if (!network) {
message("Failed to get network\n");
return_message = strdup("Failed to get network");
result = FALSE;
goto update_arp_end;
}
if (check_item_exists(config_data.networks, network) == FALSE) {
message("Network (%s is not supported\n", network);
return_message = merge_strings(3, "Network (", network , " is not supported");
result = FALSE;
goto update_arp_end;
}
unique = create_unique_id();
arp_file_name = merge_strings(3, config_data.mac_dir, network, ".mac");
tmp_file_name = merge_strings(2, config_data.mac_dir, unique);
arp_file = fopen(arp_file_name, "r");
if (!arp_file) {
message("Failed to open arp_file: %s\n", arp_file_name);
return_message = strdup("Failed to open arp_file");
result = FALSE;
goto update_arp_end;
}
tmpf = fopen(tmp_file_name, "w+");
if (!tmpf) {
fclose(arp_file);
message("Failed to open temporary_file: %s\n", tmp_file_name);
return_message = strdup("Failed to open temporary_file");
result = FALSE;
goto update_arp_end;
}
file_updated = FALSE;
while (!feof(arp_file)) {
buff = read_string(arp_file);
if (!buff)
break;
tmp = get_word_by_number(buff, 1);
if (!tmp) {
fprintf(tmpf, "%s\n", buff);
if (buff) safe_free(&buff);
continue;
}
// Searching mode...
if (strcmp(tmp, ip) == 0) {
file_updated = TRUE;
safe_free(&tmp);
tmp = merge_strings(4, ip, "\t", mac, "\tpub");
fprintf(tmpf, "%s\n", tmp);
}
else {
fprintf(tmpf, "%s\n", buff);
}
if (buff) safe_free(&buff);
if (tmp) safe_free(&tmp);
}
fclose(arp_file);
fclose(tmpf);
// END OF PARSE LIST OF ZONES
if (rename(tmp_file_name, arp_file_name) == -1) {
message("Failed to Rename file\n");
return_message = strdup("Failed to Rename file");
result = FALSE;
goto update_arp_end;
}
if (file_updated != TRUE) {
message("File was not updated\n");
return_message = strdup("File was not updated");
result = FALSE;
goto update_arp_end;
}
if (ROOT_COMPILE) {
// We should reload arp...
fork_res = vfork();
if (fork_res == 0){
// After fork here child thread
char *args[4] = {ARP_PATH, "-d", ip, NULL};
execve(ARP_PATH, args, NULL);
_exit(EXIT_SUCCESS);
}
if (fork_res != 0) {
wait(NULL);
}
fork_res = vfork();
if (fork_res == 0){
// After fork here child thread
#ifdef FREEBSD
char *args[6] = {ARP_PATH, "-s", ip, mac, "pub", NULL};
#else
char *args[5] = {ARP_PATH, "-s", ip, mac, NULL};
#endif
execve(ARP_PATH, args, NULL);
_exit(EXIT_SUCCESS);
}
if (fork_res != 0) {
wait(NULL);
}
}
result = TRUE;
update_arp_end:
if (ip) safe_free(&ip);
if (mac) safe_free(&mac);
if (network) safe_free(&network);
if (arp_file_name) safe_free(&arp_file_name);
if (tmp_file_name) safe_free(&tmp_file_name);
if (buff) safe_free(&buff);
if (tmp) safe_free(&tmp);
if (unique) safe_free(&unique);
return result;
}
unsigned int delete_arp(char * cmd)
{
unsigned int result = FALSE;
int fork_res;
char * ip = NULL;
char * network = NULL;
unsigned int k =0, c = 0;
ip = get_word_by_number(cmd, 3);
if (!ip) {
result = FALSE;
goto delete_arp_end;
}
if (check_item_exists(config_data.denied_for, ip) == TRUE) {
return_message = strdup("IP ADDRESS IS NOT ALLOWED");
message("IP ADDRESS IS NOT ALLOWED: %s\n", ip);
result = FALSE;
goto delete_arp_end;
};
k = 0; c = 0;
while (strlen(ip) > k && c != 2) {
if (ip[k] == '.')
c++;
k++;
}
network = add_char_to_string(network, ip[k++]);
while (ip[k] && ip[k] != '.') {
network = add_char_to_string(network, ip[k++]);
}
if (!network) {
message("Failed to get network\n");
return_message = strdup("Failed to get network");
result = FALSE;
goto delete_arp_end;
}
if (check_item_exists(config_data.networks, network) == FALSE) {
message("Network (%s is not supported\n", network);
return_message = merge_strings(3, "Network (", network , " is not supported");
result = FALSE;
goto delete_arp_end;
}
if (ROOT_COMPILE) {
// We should reload arp...
fork_res = vfork();
if (fork_res == 0){
// After fork here child thread
char *args[4] = {ARP_PATH, "-d", ip, NULL};
execve(ARP_PATH, args, NULL);
_exit(EXIT_SUCCESS);
}
if (fork_res != 0) {
wait(NULL);
}
}
result = TRUE;
delete_arp_end:
if (ip) safe_free(&ip);
if (network) safe_free(&network);
return result;
}