File: rules.cpp

package info (click to toggle)
fireflier 1.1.6-3etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 3,348 kB
  • ctags: 1,167
  • sloc: sh: 9,023; cpp: 8,370; makefile: 437; ansic: 300
file content (445 lines) | stat: -rw-r--r-- 12,663 bytes parent folder | download
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#include "../include/fireflier.h"
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "rules.h"

char *CHAINS[]={"", "INPUT", "FORWARD", "OUTPUT"};
static char *IPTABLES[3]={"/usr/sbin/iptables", "/sbin/iptables", "/usr/local/sbin/iptables"};
int iptables_path=-1; // index in IPTABLES

using namespace std;

int initrules()
{
    struct stat filedes;
    iptables_path=0;
    while (iptables_path<3)
    {
	if (stat(IPTABLES[iptables_path], &filedes)!=-1)
	{
            return 1; // success
	}
        iptables_path++;
	}
    iptables_path=-1;
    return 0; // failure
}

// get position of queue target in this chain. (sothat we can insert new rules immediately before)
// if there are more than one queue targets, we use the first one
int getPos(int chainid)
{
    int counter=-1;
    char *rules=getChains(); // get all iptables rules
    char line[150];
    char *pos, *nlpos;

    // valid chain ?
    if ((chainid>=4) || (chainid<=0))
    {
        cout << "RULES LINE " << __LINE__ << ": INVALID CHAIN ID" << endl;
        return -1;
    }

    if (!rules)
        return -1;

    pos=rules;
    // now I parse the rules. as soon as beginning of desired chain is reached, I start a counter
    // count until queue rule reached --> return counter
    do {
        nlpos=strchr(pos, '\n'); // divide into lines
        if (nlpos)
        {
            if (nlpos-pos>150) // just for sure
                return -1;
            strncpy(line, pos, nlpos-pos); 
            line[nlpos-pos]=0;

            if (strstr(line, CHAINS[chainid])!=NULL) // beginning of desired chain?
            {
                counter=0;
            }
            else if ((strstr(line, "QUEUE")!=NULL) && (counter!=-1)) // queue target reached
            {
                nlpos=0; // to exit the loop
            }
            else if (counter>=0) // counting activated?
            {
                counter++; 
            }
            pos=nlpos+1;
        }
    } while (nlpos);
    delete[] rules;
    return counter;
}

// input a rule by various parameters. if any parameter is 0, it is ignored
int inputRule(unsigned int chainid, int protocol, unsigned long int srcip, unsigned long int dstip, int srcport, int dstport, char *interface_in, char *interface_out, int conntrack, int action)
{
    char cmd[300];
    char revcmd[300];
    char temp[30];
    int pos;

    // should never happen
    if (iptables_path==-1)
        return 0;

    // valid rule ?
    if ((action!=ff::ACTION_QUEUE) && !protocol && !srcip && !dstip && !srcport && !dstport && !interface_in &&
        !interface_out)
        return 0;

    // valid chain ?
    if ((chainid>=4) || (chainid<=0))
    {
        cout << "RULES LINE " << __LINE__ << ": INVALID CHAIN ID" << endl;
        return 0;
    }
    
    // inserting of QUEUE rule?
    if (action==ff::ACTION_QUEUE) // create queue rule as last rule in firewall
    {
        strcpy(cmd, IPTABLES[iptables_path]);
        strcat(cmd, " -A ");
        strcpy(revcmd, IPTABLES[iptables_path]);
        strcat(revcmd, " -A ");
    }
    else // others create at position queue-1 (calculation of position follows
    {
        strcpy(cmd, IPTABLES[iptables_path]);
        strcat(cmd, " -I ");
        strcpy(revcmd, IPTABLES[iptables_path]);
        strcat(revcmd, " -I ");
    }
    strcat(cmd, CHAINS[chainid]); // chain identifier
    strcat(revcmd, CHAINS[4-chainid]);
    
    if (action!=ff::ACTION_QUEUE) // for ff::ACTION_QUEUE not needed (append)
    {
        pos=getPos(chainid); // position queue-1
        if (pos==-1) // should not happen
        {
            cout << "Position not found!!!" << endl;
            return 0;
        }
        sprintf(temp, " %d ", pos);
        strcat(cmd, temp);
        
        pos=getPos(4-chainid); // position queue-1
        if (pos==-1) // should not happen
        {
            cout << "Position not found!!!" << endl;
            return 0;
        }
        sprintf(temp, " %d ", pos);
        strcat(revcmd, temp);
    }

    if (protocol!=0) // handle protocols (number --> protocol string)
    {
        switch (protocol)
        {
	case ff::PROT_TCP:
            strcat(cmd, "-p tcp");
            strcat(revcmd, "-p tcp");
            break;
	case ff::PROT_UDP:
            strcat(cmd, "-p udp");
            strcat(revcmd, "-p udp");
            break;
	case ff::PROT_ICMP:
            strcat(cmd, "-p icmp");
            strcat(revcmd, "-p icmp");
            break;
	case ff::PROT_GRE:
            strcat(cmd, "-p gre");
            strcat(revcmd, "-p gre");
            break;
        default:
            break;
        }
    }

    if (srcip!=0) // match Source ip of packets
    {
        strcat(cmd, " -s ");
        strcat(revcmd, " -d ");
        sprintf(temp, "%d.%d.%d.%d", ((unsigned char*)&srcip)[0], ((unsigned char*)&srcip)[1], ((unsigned char*)&srcip)[2], ((unsigned char*)&srcip)[3]);
        strcat(cmd, temp);
        strcat(revcmd, temp);
    }
    if (dstip!=0) // match Destination ip of packets
    {
        strcat(cmd, " -d ");
        strcat(revcmd, " -s ");
        sprintf(temp, "%d.%d.%d.%d", ((unsigned char*)&dstip)[0], ((unsigned char*)&dstip)[1], ((unsigned char*)&dstip)[2], ((unsigned char*)&dstip)[3]);
        strcat(cmd, temp);
        strcat(revcmd, temp);
    }

    if (srcport!=0) // match source port
    {
        sprintf(temp, " --source-port %d", srcport);
        strcat(cmd, temp);
//        sprintf(temp, " --destination-port %d", srcport);
//        strcat(revcmd, temp);
    }
    if (dstport!=0) // match destination port
    {
        sprintf(temp, " --destination-port %d", dstport);
        strcat(cmd, temp);
//        sprintf(temp, " --source-port %d", dstport);
//        strcat(revcmd, temp);
    }

    if (interface_in!=0) // match incoming interface names
    {
        sprintf(temp, " --in-interface %s", interface_in);
        strcat(cmd, temp);
        sprintf(temp, " --out-interface %s", interface_in);
        strcat(revcmd, temp);
    }
    if (interface_out!=0) // match outgoing interface names
    {
        sprintf(temp, " --out-interface %s", interface_out);
        strcat(cmd, temp);
        sprintf(temp, " --in-interface %s", interface_out);
        strcat(revcmd, temp);
    }

    // stateful firewalling (allow only those packets in the other direction,
    // which belong to existing connection). see connection tracking
    strcat (revcmd, " -m state --state RELATED,ESTABLISHED");

    if (action==ff::ACTION_DROP)
    {
        strcat(cmd, " -j DROP");
        strcat(revcmd, " -j DROP");
    }
    else if (action==ff::ACTION_ACCEPT)
    {
        strcat(cmd, " -j ACCEPT");
        strcat(revcmd, " -j ACCEPT");
    }
    else if (action==ff::ACTION_QUEUE)
    {
        strcat(cmd, " -j QUEUE");
        strcat(revcmd, " -j QUEUE");
    }

//    printf("%s\n", cmd);
    if (conntrack) // only if connection tracking wanted use inverted rule
    {
		system(revcmd);
    }
    system(cmd); // insert rule
    return 1;
}

// delete rule by definition. needed by timeoutthread
void deleteRule(unsigned int chainid, int protocol, unsigned long int srcip, unsigned long int dstip, int srcport, int dstport, char *interface_in, char *interface_out, int conntrack, int action)
{
    char cmd[300];
    char revcmd[300];
    char temp[30];
//    char *rules;

    // valid rule?
    if ((action!=ff::ACTION_QUEUE) && !protocol && !srcip && !dstip && !srcport && !dstport && !interface_in &&
        !interface_out)
        return;
   
    // valid chain?
    if ((chainid>=4) || (chainid<=0))
    {
        cout << "RULES LINE " << __LINE__ << ": INVALID CHAIN ID" << endl;
        return;
    }


    strcpy(cmd, IPTABLES[iptables_path]);
    strcat(cmd, " -D ");
    strcpy(revcmd, IPTABLES[iptables_path]);
    strcat(revcmd, " -D ");
    strcat(cmd, CHAINS[chainid]);
    strcat(revcmd, CHAINS[4-chainid]);
    strcat(cmd, " ");
    strcat(revcmd, " ");

    if (protocol!=0) // protocols (number --> name)
    {
        switch (protocol)
        {
	case ff::PROT_TCP:
            strcat(cmd, "-p tcp");
            strcat(revcmd, "-p tcp");
            break;
        case ff::PROT_UDP:
            strcat(cmd, "-p udp");
            strcat(revcmd, "-p udp");
            break;
        case ff::PROT_ICMP:
            strcat(cmd, "-p icmp");
            strcat(revcmd, "-p icmp");
            break;
        case ff::PROT_GRE:
            strcat(cmd, "-p gre");
            strcat(revcmd, "-p gre");
            break;
        default:
            break;
        }
    }

    if (srcip!=0) // source of packets
    {
        strcat(cmd, " -s ");
        strcat(revcmd, " -d ");
        sprintf(temp, "%d.%d.%d.%d", ((unsigned char*)&srcip)[0], ((unsigned char*)&srcip)[1], ((unsigned char*)&srcip)[2], ((unsigned char*)&srcip)[3]);
        strcat(cmd, temp);
        strcat(revcmd, temp);
    }
    if (dstip!=0) // destination of packets
    {
        strcat(cmd, " -d ");
        strcat(revcmd, " -s ");
        sprintf(temp, "%d.%d.%d.%d", ((unsigned char*)&dstip)[0], ((unsigned char*)&dstip)[1], ((unsigned char*)&dstip)[2], ((unsigned char*)&dstip)[3]);
        strcat(cmd, temp);
        strcat(revcmd, temp);
    }

    if (srcport!=0) // source port of packets
    {
        sprintf(temp, " --source-port %d", srcport);
        strcat(cmd, temp);
//        sprintf(temp, " --destination-port %d", srcport);
//        strcat(revcmd, temp);
    }
    if (dstport!=0) // destination port of packets
    {
        sprintf(temp, " --destination-port %d", dstport);
        strcat(cmd, temp);
//        sprintf(temp, " --source-port %d", dstport);
//        strcat(revcmd, temp);
    }

    if (interface_in!=0) // incoming interface
    {
        sprintf(temp, " --in-interface %s", interface_in);
        strcat(cmd, temp);
        sprintf(temp, " --out-interface %s", interface_in);
        strcat(revcmd, temp);
    }
    if (interface_out!=0) // ougoing interface
    {
        sprintf(temp, " --out-interface %s", interface_out);
        strcat(cmd, temp);
        sprintf(temp, " --in-interface %s", interface_out);
        strcat(revcmd, temp);
    }

    // connection tracking
    strcat (revcmd, " -m state --state RELATED,ESTABLISHED");

    if (action==ff::ACTION_DROP)
    {
        strcat(cmd, " -j DROP");
        strcat(revcmd, " -j DROP");
    }
    else if (action==ff::ACTION_ACCEPT)
    {
        strcat(cmd, " -j ACCEPT");
        strcat(revcmd, " -j ACCEPT");
    }
    else if (action==ff::ACTION_QUEUE)
    {
        strcat(cmd, " -j QUEUE");
        strcat(revcmd, " -j QUEUE");
    }

    system(cmd);

    // corresponding inverted rule if connection tracking is enabled
    if (conntrack)
    {
        system(revcmd);
    }
}

// delete rule by position in chain
void deleteRule(unsigned int chainid, int rulenum)
{
    char rule[8];
    char cmd[80];

    //should never happen
    if (iptables_path==-1)
        return;

    // valid chain?
    if ((chainid>3) || (chainid<1)) return;

    strcpy(cmd, IPTABLES[iptables_path]);
    strcat(cmd, " -D ");
    strcat(cmd, CHAINS[chainid]);

    sprintf(rule, " %d ", rulenum);
    strcat(cmd, rule);

    system(cmd);
}

// get all firewall chains as returned by iptables -L -n -v --line-numbers
// this output is used for the interface and for finding rules in the chains
char *getChains()
{
    char *buffer=0;
    int filesize;
    struct stat filedes;
    int handle;
    char cmd[200];

    // create file with output of iptables (I use this as it is easier than using libiptc
    // and iptables itself should work more reliable than implementing this functionality myself)
    strcpy(cmd, "rm -f /var/run/fireflier/fireflier.rules && touch /var/run/fireflier/fireflier.rules && chmod 0700 /var/run/fireflier/fireflier.rules && ");
    strcat(cmd, IPTABLES[iptables_path]);
    strcat(cmd, " -v -L -n --line-numbers > /var/run/fireflier/fireflier.rules");
    system(cmd);

    // should usually work
    if (stat("/var/run/fireflier/fireflier.rules", &filedes)==-1)
        return 0;

    filesize=filedes.st_size;

    buffer=new char[filesize+1];
    if (!buffer)
    {
        cout << "Out of memory" << endl;
        return 0;
    }
    buffer[0]=0;
    buffer[filesize]=0;

    // open rules file
    if ((handle=open("/var/run/fireflier/fireflier.rules", O_RDONLY))==-1)
        return 0;

    if (read(handle, buffer, filesize)==-1)
        return 0;

    close(handle);
    // finally delete the rules file to clean up
    unlink("/var/run/fireflier/fireflier.rules");

    return buffer;
}