diff options
author | 2022-01-07 09:52:00 -0500 | |
---|---|---|
committer | 2022-01-07 09:52:00 -0500 | |
commit | 500a56efd310396f142440019aee671b5f747efb (patch) | |
tree | 8effc272b3814207c8b5583e99bcd9b925558dab /src/fnettrace/hostnames.c | |
parent | fix wrap/nowrap help string in firemon (diff) | |
download | firejail-500a56efd310396f142440019aee671b5f747efb.tar.gz firejail-500a56efd310396f142440019aee671b5f747efb.tar.zst firejail-500a56efd310396f142440019aee671b5f747efb.zip |
more on nettrace
Diffstat (limited to 'src/fnettrace/hostnames.c')
-rw-r--r-- | src/fnettrace/hostnames.c | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/src/fnettrace/hostnames.c b/src/fnettrace/hostnames.c new file mode 100644 index 000000000..e5fbb6d20 --- /dev/null +++ b/src/fnettrace/hostnames.c | |||
@@ -0,0 +1,120 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2014-2021 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 "fnettrace.h" | ||
21 | #include "radix.h" | ||
22 | #define MAXBUF 1024 | ||
23 | |||
24 | |||
25 | char *retrieve_hostname(uint32_t ip) { | ||
26 | char *rv = NULL; | ||
27 | char *cmd; | ||
28 | if (asprintf(&cmd, "/usr/bin/geoiplookup %d.%d.%d.%d", PRINT_IP(ip)) == -1) | ||
29 | errExit("asprintf"); | ||
30 | |||
31 | FILE *fp = popen(cmd, "r"); | ||
32 | if (fp) { | ||
33 | char *ptr; | ||
34 | char buf[MAXBUF]; | ||
35 | if (fgets(buf, MAXBUF, fp)) { | ||
36 | ptr = strchr(buf, '\n'); | ||
37 | if (ptr) | ||
38 | *ptr = '\0'; | ||
39 | if (strncmp(buf, "GeoIP Country Edition:", 22) == 0) { | ||
40 | ptr = buf + 22; | ||
41 | if (*ptr == ' ' && *(ptr + 3) == ',' && *(ptr + 4) == ' ') { | ||
42 | rv = ptr + 5; | ||
43 | radix_add(ip, 0xffffffff, ptr + 5); | ||
44 | } | ||
45 | } | ||
46 | } | ||
47 | fclose(fp); | ||
48 | return rv; | ||
49 | } | ||
50 | |||
51 | return NULL; | ||
52 | } | ||
53 | |||
54 | void load_hostnames(const char *fname) { | ||
55 | assert(fname); | ||
56 | FILE *fp = fopen(fname, "r"); | ||
57 | if (!fp) { | ||
58 | fprintf(stderr, "Warning: cannot find %s file\n", fname); | ||
59 | return; | ||
60 | } | ||
61 | |||
62 | char buf[MAXBUF]; | ||
63 | int line = 0; | ||
64 | while (fgets(buf, MAXBUF, fp)) { | ||
65 | line++; | ||
66 | |||
67 | // skip empty spaces | ||
68 | char *start = buf; | ||
69 | while (*start == ' ' || *start == '\t') | ||
70 | start++; | ||
71 | // comments | ||
72 | if (*start == '#') | ||
73 | continue; | ||
74 | char *end = strchr(start, '#'); | ||
75 | if (end) | ||
76 | *end = '\0'; | ||
77 | |||
78 | // end | ||
79 | end = strchr(start, '\n'); | ||
80 | if (end) | ||
81 | *end = '\0'; | ||
82 | end = start + strlen(start); | ||
83 | if (end == start) // empty line | ||
84 | continue; | ||
85 | |||
86 | // line format: 1.2.3.4/32 name_without_empty_spaces | ||
87 | // a single empty space between address and name | ||
88 | end = strchr(start, ' '); | ||
89 | if (!end) | ||
90 | goto errexit; | ||
91 | *end = '\0'; | ||
92 | end++; | ||
93 | if (*end == '\0') | ||
94 | goto errexit; | ||
95 | |||
96 | uint32_t ip; | ||
97 | uint32_t mask; | ||
98 | if (atocidr(start, &ip, &mask)) { | ||
99 | fprintf(stderr, "Error: invalid CIDR address\n"); | ||
100 | goto errexit; | ||
101 | } | ||
102 | |||
103 | radix_add(ip, mask, end); | ||
104 | } | ||
105 | |||
106 | fclose(fp); | ||
107 | return; | ||
108 | |||
109 | |||
110 | errexit: | ||
111 | fprintf(stderr, "Error: invalid line %d in file %s\n", line, fname); | ||
112 | exit(1); | ||
113 | } | ||
114 | |||
115 | void build_list(const char *fname) { | ||
116 | assert(fname); | ||
117 | load_hostnames(fname); | ||
118 | radix_build_list(); | ||
119 | } | ||
120 | |||