diff options
author | netblue30 <netblue30@yahoo.com> | 2017-11-13 11:04:40 -0500 |
---|---|---|
committer | netblue30 <netblue30@yahoo.com> | 2017-11-13 11:04:40 -0500 |
commit | 7c305841e68d39f19ee5c0093fdfc8ca2e65e215 (patch) | |
tree | f4f59e81f4024cd1f82b2bca1b504dbe108fb673 /src/fnetfilter/main.c | |
parent | netfilter split (diff) | |
download | firejail-7c305841e68d39f19ee5c0093fdfc8ca2e65e215.tar.gz firejail-7c305841e68d39f19ee5c0093fdfc8ca2e65e215.tar.zst firejail-7c305841e68d39f19ee5c0093fdfc8ca2e65e215.zip |
netfilter split
Diffstat (limited to 'src/fnetfilter/main.c')
-rw-r--r-- | src/fnetfilter/main.c | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/src/fnetfilter/main.c b/src/fnetfilter/main.c new file mode 100644 index 000000000..67ab31832 --- /dev/null +++ b/src/fnetfilter/main.c | |||
@@ -0,0 +1,115 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2014-2017 Firejail Authors | ||
3 | * | ||
4 | * This file is part of firejail project | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU General Public License as published by | ||
8 | * the Free Software Foundation; either version 2 of the License, or | ||
9 | * (at your option) any later version. | ||
10 | * | ||
11 | * This program is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | * GNU General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU General Public License along | ||
17 | * with this program; if not, write to the Free Software Foundation, Inc., | ||
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
19 | */ | ||
20 | #include "../include/common.h" | ||
21 | |||
22 | #define MAXBUF 4098 | ||
23 | int arg_quiet = 0; | ||
24 | |||
25 | static char *default_filter = | ||
26 | "*filter\n" | ||
27 | ":INPUT DROP [0:0]\n" | ||
28 | ":FORWARD DROP [0:0]\n" | ||
29 | ":OUTPUT ACCEPT [0:0]\n" | ||
30 | "-A INPUT -i lo -j ACCEPT\n" | ||
31 | "-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT\n" | ||
32 | "# echo replay is handled by -m state RELATED/ESTABLISHED below\n" | ||
33 | "#-A INPUT -p icmp --icmp-type echo-reply -j ACCEPT\n" | ||
34 | "-A INPUT -p icmp --icmp-type destination-unreachable -j ACCEPT\n" | ||
35 | "-A INPUT -p icmp --icmp-type time-exceeded -j ACCEPT\n" | ||
36 | "-A INPUT -p icmp --icmp-type echo-request -j ACCEPT \n" | ||
37 | "# disable STUN\n" | ||
38 | "-A OUTPUT -p udp --dport 3478 -j DROP\n" | ||
39 | "-A OUTPUT -p udp --dport 3479 -j DROP\n" | ||
40 | "-A OUTPUT -p tcp --dport 3478 -j DROP\n" | ||
41 | "-A OUTPUT -p tcp --dport 3479 -j DROP\n" | ||
42 | "COMMIT\n"; | ||
43 | |||
44 | static void usage(void) { | ||
45 | printf("Usage:\n"); | ||
46 | printf("\tfnetfilter netfilter-command destination-file\n"); | ||
47 | } | ||
48 | |||
49 | int main(int argc, char **argv) { | ||
50 | #if 0 | ||
51 | { | ||
52 | system("cat /proc/self/status"); | ||
53 | int i; | ||
54 | for (i = 0; i < argc; i++) | ||
55 | printf("*%s* ", argv[i]); | ||
56 | printf("\n"); | ||
57 | } | ||
58 | #endif | ||
59 | |||
60 | char *quiet = getenv("FIREJAIL_QUIET"); | ||
61 | if (quiet && strcmp(quiet, "yes") == 0) | ||
62 | arg_quiet = 1; | ||
63 | |||
64 | if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") ==0) { | ||
65 | usage(); | ||
66 | return 0; | ||
67 | } | ||
68 | |||
69 | if (argc != 2 && argc != 3) { | ||
70 | usage(); | ||
71 | return 1; | ||
72 | } | ||
73 | |||
74 | char *destfile = (argc == 3)? argv[2]: argv[1]; | ||
75 | char *command = (argc == 3)? argv[1]: NULL; | ||
76 | //printf("command %s\n", command); | ||
77 | //printf("destfile %s\n", destfile); | ||
78 | |||
79 | // handle default config (command = NULL, destfile) | ||
80 | if (command == NULL) { | ||
81 | // create a default filter file | ||
82 | FILE *fp = fopen(destfile, "w"); | ||
83 | if (!fp) { | ||
84 | fprintf(stderr, "Error fnetfilter: cannot open %s\n", destfile); | ||
85 | exit(1); | ||
86 | } | ||
87 | fprintf(fp, "%s\n", default_filter); | ||
88 | fclose(fp); | ||
89 | } | ||
90 | else { | ||
91 | // copy the file | ||
92 | FILE *fp1 = fopen(command, "r"); | ||
93 | if (!fp1) { | ||
94 | fprintf(stderr, "Error fnetfilter: cannot open %s\n", command); | ||
95 | exit(1); | ||
96 | } | ||
97 | |||
98 | FILE *fp2 = fopen(destfile, "w"); | ||
99 | if (!fp2) { | ||
100 | fprintf(stderr, "Error fnetfilter: cannot open %s\n", destfile); | ||
101 | exit(1); | ||
102 | } | ||
103 | |||
104 | char buf[MAXBUF]; | ||
105 | while (fgets(buf, MAXBUF, fp1)) | ||
106 | fprintf(fp2, "%s", buf); | ||
107 | |||
108 | fclose(fp1); | ||
109 | fclose(fp2); | ||
110 | } | ||
111 | |||
112 | |||
113 | printf("fnetfilter running\n"); | ||
114 | return 0; | ||
115 | } | ||