-
Notifications
You must be signed in to change notification settings - Fork 0
/
port-open.c
382 lines (327 loc) · 8.1 KB
/
port-open.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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/**
* Compile:
* gcc -s -o port-open port-open.c
* cp -a port-open /usr/local/sbin
* cd /usr/local/sbin
* ln -sf port-open port-close
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <libgen.h>
#include <sys/types.h>
#include <unistd.h>
#define IPTABLES_OPEN_CMD "/sbin/iptables -A knock -s %s -p tcp --dport %d -j ACCEPT >/dev/null"
#define IPTABLES_CLOSE_CMD "/sbin/iptables -D knock -s %s -p tcp --dport %d -j ACCEPT >/dev/null 2>&1"
#define IPTABLES_ALLOW_CMD "/sbin/iptables -A knock_allow -s %s -j ACCEPT >/dev/null"
#define IPTABLES_REMOVE_CMD "/sbin/iptables -D knock_allow -s %s -j ACCEPT >/dev/null 2>&1"
#define IPTABLES_BLOCK_CMD "/sbin/iptables -A knock_block -s %s -j DROP >/dev/null"
#define IPTABLES_UNBLOCK_CMD "/sbin/iptables -D knock_block -s %s -j DROP >/dev/null 2>&1"
#define PROG_PORT_OPEN 1
#define PROG_PORT_CLOSE 2
#define PROG_IP_ALLOW 3
#define PROG_IP_REMOVE 4
#define PROG_IP_BLOCK 5
#define PROG_IP_UNBLOCK 6
char *name;
char progPath[1026];
int program = 1;
long timeout = 0;
bool verifyIp(const char *ip);
bool verifyPort(long port);
void usageOpen(){
printf("Adds ACCEPT rule for a given IP and port using iptables. If timeout is grater than 0 starts port-close with the same parameters:\n");
printf("Usage:\n");
printf(" %s <ip> <port> [timeout=0]\n", name);
}
void usageClose(){
printf("Removes the iptables ACCEPT rule, previously added with port-open after timeout seconds.\n");
printf("Usage:\n");
printf(" %s <ip> <port> [timeout=0]\n", name);
}
void usageIpAdd(int action) {
const char *ruleName;
const char *removeProgName;
switch(action) {
case PROG_IP_ALLOW:
ruleName = "ACCEPT";
removeProgName = "ip-remove";
break;
case PROG_IP_BLOCK:
ruleName = "REJECT";
removeProgName = "ip-unblock";
break;
default:
printf("Bad action: %d\n", action);
return;
}
printf("Adds %s rule for a given IP using iptables. If timeout is grater than 0 starts %s with the same parameters:\n", ruleName, removeProgName);
printf("Usage:\n");
printf(" %s <ip> [timeout=0]\n", name);
}
void usageIpRemove(int action){
const char *ruleName;
const char *addProgName;
switch(action) {
case PROG_IP_REMOVE:
ruleName = "ACCEPT";
addProgName = "ip-remove";
break;
case PROG_IP_UNBLOCK:
ruleName = "REJECT";
addProgName = "ip-unblock";
break;
default:
printf("Bad action: %d\n", action);
return;
}
printf("Removes the iptables %s rule, previously added with %s after timeout seconds.\n", ruleName, addProgName);
printf("Usage:\n");
printf(" %s <ip> [timeout=0]\n", name);
}
void usage() {
switch(program){
case PROG_PORT_OPEN:
usageOpen();
break;
case PROG_PORT_CLOSE:
usageClose();
break;
}
}
int portOpen(int argc, char **argv){
long tmp;
char **endPtr = NULL;
if(argc < 3){
usageOpen();
return 1;
}
const char *ip = argv[1];
if(!verifyIp(ip))
return 1;
tmp = strtol(argv[2], endPtr, 10);
const int port = (int) tmp;
if(!verifyPort(port))
return 1;
if(argc > 3){
timeout=strtol(argv[3], endPtr, 10);
}
char *cmd = malloc(1500);
sprintf(cmd, IPTABLES_OPEN_CMD, ip, (int)port);
// printf("running cmd: %s\n", cmd);
system(cmd);
if(timeout > 0) {
sprintf(cmd, "%sport-close %s %d %d", progPath, ip, (int)port, (int)timeout);
// printf("port close cmd: %s\n", cmd);
system(cmd);
}
free(cmd);
return 0;
}
int portClose(int argc, char **argv){
long tmp;
char **endPtr = NULL;
//printf("%d\n", argc);
if(argc < 3){
usageClose();
return 1;
}
const char *ip = argv[1];
if(!verifyIp(ip))
return 1;
tmp = strtol(argv[2], endPtr, 10);
const int port = (int) tmp;
if(!verifyPort(port))
return 1;
if(argc > 3){
timeout=strtol(argv[3], endPtr, 10);
}
//printf("%d\n", timeout);
if(timeout > 0) {
// drop stdout and stderr
close(STDOUT_FILENO);
close(STDERR_FILENO);
if(fork() != 0) {
// parent
return 0;
}
sleep(timeout);
}
char *cmd = malloc(1500);
sprintf(cmd, IPTABLES_CLOSE_CMD, ip, port);
// printf("running cmd: %s\n", cmd);
system(cmd);
free(cmd);
return 0;
}
bool verifyIp(const char *ip){
if(strlen(ip)<7){
printf("IP should be least 7 characters\n");
usage();
return false;
}
if(strlen(ip)>15){
printf("IP should be max 15 characters\n");
usage();
return false;
}
return true;
}
bool verifyPort(long port) {
if(port<0) {
printf("port cannot be negative");
usage();
return false;
}
if(port>65535) {
printf("port must be less than 65535");
usage();
return false;
}
if(port==0) {
printf("Port must be grater than 0");
usage();
return false;
}
return true;
}
int ipAllowBlock(int action, int argc, char **argv){
char **endPtr = NULL;
if(argc < 2){
usageIpAdd(action);
return 1;
}
const char *ip = argv[1];
if(!verifyIp(ip))
return 1;
if(argc > 2){
timeout=strtol(argv[2], endPtr, 10);
}
char *cmd = malloc(1500);
const char *cmdTemplate = NULL;
const char *closeCmdTemplate;
switch(action) {
case PROG_IP_ALLOW:
cmdTemplate = IPTABLES_ALLOW_CMD;
closeCmdTemplate = "%sip-remove %s %d";
break;
case PROG_IP_BLOCK:
cmdTemplate = IPTABLES_BLOCK_CMD;
closeCmdTemplate = "%sip-unblock %s %d";
break;
default:
printf("ipAllowBlock(): invalid action %d\n", action);
return 1;
}
if (cmdTemplate) {
sprintf(cmd, cmdTemplate, ip);
// printf("running cmd: %s\n", cmd);
system(cmd);
if (timeout > 0) {
sprintf(cmd, closeCmdTemplate, progPath, ip, (int) timeout);
// printf("ip remove cmd: %s\n", cmd);
system(cmd);
}
}
free(cmd);
if (!cmdTemplate) {
printf("Invalid action %d\n", action);
}
return 0;
}
int ipRemoveUnblock(int action, int argc, char **argv){
char **endPtr = NULL;
//printf("%d\n", argc);
if(argc < 2){
usageIpRemove(action);
return 1;
}
const char *ip = argv[1];
if(!verifyIp(ip))
return 1;
if(argc > 2){
timeout=strtol(argv[2], endPtr, 10);
}
const char *cmdTemplate = NULL;
switch(action) {
case PROG_IP_REMOVE:
cmdTemplate = IPTABLES_REMOVE_CMD;
break;
case PROG_IP_UNBLOCK:
cmdTemplate = IPTABLES_UNBLOCK_CMD;
break;
}
//printf("%d\n", timeout);
if(timeout > 0) {
// drop stdout and stderr
close(STDOUT_FILENO);
close(STDERR_FILENO);
if(fork() != 0) {
// parent
return 0;
}
// child
sleep(timeout);
}
char *cmd = malloc(1500);
sprintf(cmd, cmdTemplate, ip);
// printf("running cmd: %s\n", cmd);
system(cmd);
free(cmd);
return 0;
}
int main(int argc, char **argv){
if(argc<1){
printf("argc < 1 !!!\n");
}
name = basename(argv[0]);
if(strcmp(name, "port-open") == 0){
program = PROG_PORT_OPEN;
}else if(strcmp(name, "port-close") == 0) {
program = PROG_PORT_CLOSE;
}else if(strcmp(name, "ip-allow") == 0) {
program = PROG_IP_ALLOW;
}else if(strcmp(name, "ip-remove") == 0) {
program = PROG_IP_REMOVE;
}else if(strcmp(name, "ip-block") == 0) {
program = PROG_IP_BLOCK;
}else if(strcmp(name, "ip-unblock") == 0) {
program = PROG_IP_UNBLOCK;
}
char *dirTemp = dirname(argv[0]);
if (strlen(dirTemp) > 1024) {
printf("Can't work with paths longer than 1024 chars\n");
return 1;
}
// if(strchr(argv[0], '/') == NULL) {
if(false) {
progPath[0] = 0;
}else {
strncpy(progPath, dirTemp, 1024);
int len = strlen(progPath);
progPath[len] = '/';
progPath[len+1] = 0;
}
// printf("path is %s\n", progPath);
switch(program){
case PROG_PORT_OPEN:
portOpen(argc, argv);
break;
case PROG_PORT_CLOSE:
portClose(argc, argv);
break;
case PROG_IP_ALLOW:
case PROG_IP_BLOCK:
ipAllowBlock(program, argc, argv);
break;
case PROG_IP_REMOVE:
case PROG_IP_UNBLOCK:
ipRemoveUnblock(program, argc, argv);
break;
default:
printf("Invalid program number %d", program);
}
return 0;
}