Lines 1-165
Link Here
|
1 |
--- doorman-0.8.orig/doormand.c Thu Jul 29 21:24:02 2004 |
1 |
--- doorman-0.81/doormand.c 2005-10-30 14:00:27.000000000 +0100 |
2 |
+++ doorman-0.8/doormand.c Sun May 29 09:05:31 2005 |
2 |
+++ doormand.c 2005-10-30 14:05:18.000000000 +0100 |
3 |
@@ -397,7 +397,11 @@ |
3 |
@@ -387,7 +387,11 @@ |
4 |
int datalink_header_lengths[] = { |
4 |
int datalink_header_lengths[] = { |
5 |
// hdr len code data link type |
5 |
// hdr len code data link type |
6 |
// ------- --- --------------------------- |
6 |
// ------- --- --------------------------- |
7 |
+#ifdef __FreeBSD__ |
7 |
+#ifdef __FreeBSD__ |
8 |
+ 4, // 0 no link-layer encapsulation |
8 |
+ 4, // 0 no link-layer encapsulation |
9 |
+#else |
9 |
+#else |
10 |
0, // 0 no link-layer encapsulation |
10 |
0, // 0 no link-layer encapsulation |
11 |
+#endif |
11 |
+#endif |
12 |
14, // 1 Ethernet (10Mb) |
12 |
14, // 1 Ethernet (10Mb) |
13 |
-1, // 2 Experimental Ethernet (3Mb) |
13 |
-1, // 2 Experimental Ethernet (3Mb) |
14 |
-1, // 3 Amateur Radio AX.25 |
14 |
-1, // 3 Amateur Radio AX.25 |
15 |
@@ -557,6 +561,14 @@ |
15 |
@@ -614,6 +618,14 @@ |
16 |
// more readable. |
16 |
// more readable. |
17 |
// |
17 |
// |
18 |
|
18 |
|
19 |
+/* |
19 |
+/* |
20 |
+// lsof on FreeBSD produces one more field. |
20 |
+// lsof on FreeBSD produces one more field. |
21 |
+// This should be rewritten to use a regular expression, anyway. |
21 |
+// This should be rewritten to use a regular expression, anyway. |
22 |
+// |
22 |
+// |
23 |
+// And who said using C++ style comments in C was good for portability?!? |
23 |
+// And who said using C++ style comments in C was good for portability?!? |
24 |
+*/ |
24 |
+*/ |
25 |
+ |
25 |
+ |
26 |
+#ifdef __FreeBSD__ |
26 |
+#ifdef __FreeBSD__ |
27 |
#define LSOF()\ |
27 |
#define LSOF()\ |
28 |
sprintf (cmd, "lsof -Pn -iTCP@%s:%s", interface_ip_str, dport_string) ;\ |
28 |
sprintf (cmd, "lsof -Pn -iTCP@%s:%s", interface_ip_str, dport_string) ;\ |
29 |
\ |
29 |
\ |
30 |
@@ -578,6 +590,7 @@ |
30 |
@@ -635,6 +647,7 @@ |
31 |
if ((p1 = token (&p2, " ")) == NULL) continue ;\ |
31 |
if ((p1 = token (&p2, " ")) == NULL) continue ;\ |
32 |
if ((p1 = token (&p2, " ")) == NULL) continue ;\ |
32 |
if ((p1 = token (&p2, " ")) == NULL) continue ;\ |
33 |
if ((p1 = token (&p2, " ")) == NULL) continue ;\ |
33 |
if ((p1 = token (&p2, " ")) == NULL) continue ;\ |
34 |
+ if ((p1 = token (&p2, " ")) == NULL) continue ;\ |
34 |
+ if ((p1 = token (&p2, " ")) == NULL) continue ;\ |
35 |
if ((p1 = token (&p2, " :")) == NULL) continue ;\ |
35 |
if ((p1 = token (&p2, " :")) == NULL) continue ;\ |
36 |
local_ip = inet_addr(p1) ;\ |
36 |
local_ip = inet_addr(p1) ;\ |
37 |
if ((p1 = token (&p2, "-")) == NULL) continue ;\ |
37 |
if ((p1 = token (&p2, "-")) == NULL) continue ;\ |
38 |
@@ -602,7 +615,53 @@ |
38 |
@@ -659,7 +672,53 @@ |
39 |
}\ |
39 |
}\ |
40 |
}\ |
40 |
}\ |
41 |
pclose(f) ; |
41 |
pclose(f) ; |
42 |
- |
42 |
- |
43 |
+#else |
43 |
+#else |
44 |
+#define LSOF()\ |
44 |
+#define LSOF()\ |
45 |
+sprintf (cmd, "lsof -Pn -iTCP@%s:%s", interface_ip_str, dport_string) ;\ |
45 |
+sprintf (cmd, "lsof -Pn -iTCP@%s:%s", interface_ip_str, dport_string) ;\ |
46 |
+\ |
46 |
+\ |
47 |
+f = popen (cmd, "r") ;\ |
47 |
+f = popen (cmd, "r") ;\ |
48 |
+if (f == NULL) {\ |
48 |
+if (f == NULL) {\ |
49 |
+ croak (errno, "Can't execute '%s'; exiting.", cmd) ;\ |
49 |
+ croak (errno, "Can't execute '%s'; exiting.", cmd) ;\ |
50 |
+}\ |
50 |
+}\ |
51 |
+\ |
51 |
+\ |
52 |
+fgets(buffer, 254, f) ; /* throw away the first line. */ \ |
52 |
+fgets(buffer, 254, f) ; /* throw away the first line. */ \ |
53 |
+while (fgets(buffer, 254, f)) {\ |
53 |
+while (fgets(buffer, 254, f)) {\ |
54 |
+ p2 = buffer ;\ |
54 |
+ p2 = buffer ;\ |
55 |
+ if ((p1 = token (&p2, " ")) == NULL) continue ;\ |
55 |
+ if ((p1 = token (&p2, " ")) == NULL) continue ;\ |
56 |
+ dname = p1 ;\ |
56 |
+ dname = p1 ;\ |
57 |
+ if ((p1 = token (&p2, " ")) == NULL) continue ;\ |
57 |
+ if ((p1 = token (&p2, " ")) == NULL) continue ;\ |
58 |
+ pid = p1 ;\ |
58 |
+ pid = p1 ;\ |
59 |
+ if ((p1 = token (&p2, " ")) == NULL) continue ;\ |
59 |
+ if ((p1 = token (&p2, " ")) == NULL) continue ;\ |
60 |
+ uname = p1 ;\ |
60 |
+ uname = p1 ;\ |
61 |
+ if ((p1 = token (&p2, " ")) == NULL) continue ;\ |
61 |
+ if ((p1 = token (&p2, " ")) == NULL) continue ;\ |
62 |
+ if ((p1 = token (&p2, " ")) == NULL) continue ;\ |
62 |
+ if ((p1 = token (&p2, " ")) == NULL) continue ;\ |
63 |
+ if ((p1 = token (&p2, " ")) == NULL) continue ;\ |
63 |
+ if ((p1 = token (&p2, " ")) == NULL) continue ;\ |
64 |
+ if ((p1 = token (&p2, " ")) == NULL) continue ;\ |
64 |
+ if ((p1 = token (&p2, " ")) == NULL) continue ;\ |
65 |
+ if ((p1 = token (&p2, " :")) == NULL) continue ;\ |
65 |
+ if ((p1 = token (&p2, " :")) == NULL) continue ;\ |
66 |
+ local_ip = inet_addr(p1) ;\ |
66 |
+ local_ip = inet_addr(p1) ;\ |
67 |
+ if ((p1 = token (&p2, "-")) == NULL) continue ;\ |
67 |
+ if ((p1 = token (&p2, "-")) == NULL) continue ;\ |
68 |
+ local_port = atoi(p1) ;\ |
68 |
+ local_port = atoi(p1) ;\ |
69 |
+ if ((p1 = token (&p2, "->:")) == NULL) continue ;\ |
69 |
+ if ((p1 = token (&p2, "->:")) == NULL) continue ;\ |
70 |
+ aptr = p1 ;\ |
70 |
+ aptr = p1 ;\ |
71 |
+ remote_ip = inet_addr(p1) ;\ |
71 |
+ remote_ip = inet_addr(p1) ;\ |
72 |
+ if ((p1 = token (&p2, " ")) == NULL) continue ;\ |
72 |
+ if ((p1 = token (&p2, " ")) == NULL) continue ;\ |
73 |
+ pptr = p1 ;\ |
73 |
+ pptr = p1 ;\ |
74 |
+ remote_port = atoi(p1) ;\ |
74 |
+ remote_port = atoi(p1) ;\ |
75 |
+ if ((p1 = token (&p2, " ()")) == NULL) continue ;\ |
75 |
+ if ((p1 = token (&p2, " ()")) == NULL) continue ;\ |
76 |
+ status = p1 ;\ |
76 |
+ status = p1 ;\ |
77 |
+\ |
77 |
+\ |
78 |
+ if ((saddr == remote_ip) &&\ |
78 |
+ if ((saddr == remote_ip) &&\ |
79 |
+ (daddr == local_ip) &&\ |
79 |
+ (daddr == local_ip) &&\ |
80 |
+ (sport == remote_port) &&\ |
80 |
+ (sport == remote_port) &&\ |
81 |
+ (dport == local_port) && \ |
81 |
+ (dport == local_port) && \ |
82 |
+ (strcmp(status, "ESTABLISHED") == 0))\ |
82 |
+ (strcmp(status, "ESTABLISHED") == 0))\ |
83 |
+ {\ |
83 |
+ {\ |
84 |
+ connected = TRUE ;\ |
84 |
+ connected = TRUE ;\ |
85 |
+ break ;\ |
85 |
+ break ;\ |
86 |
+ }\ |
86 |
+ }\ |
87 |
+}\ |
87 |
+}\ |
88 |
+pclose(f) ; |
88 |
+pclose(f) ; |
89 |
+#endif |
89 |
+#endif |
90 |
|
90 |
|
91 |
|
91 |
|
92 |
|
92 |
|
93 |
@@ -647,7 +706,11 @@ |
93 |
@@ -704,7 +763,11 @@ |
94 |
snprintf (cmd, 254, "tcp and dst port %s and src %s and dst %s", |
94 |
snprintf (cmd, 254, "tcp and dst port %s and src %s and dst %s", |
95 |
dport_string, src_addr, interface_ip_str) ; |
95 |
dport_string, src_addr, interface_ip_str) ; |
96 |
DEBUG "open a secondary pcap: '%s'", cmd) ; |
96 |
DEBUG "open a secondary pcap: '%s'", cmd) ; |
97 |
+#ifdef __FreeBSD__ |
97 |
+#ifdef __FreeBSD__ |
98 |
+ hdr_len = open_a_pcap (device, 1000, &cap, cmd) ; |
98 |
+ hdr_len = open_a_pcap (device, 1000, &cap, cmd) ; |
99 |
+#else |
99 |
+#else |
100 |
hdr_len = open_a_pcap (device, 0, &cap, cmd) ; |
100 |
hdr_len = open_a_pcap (device, 0, &cap, cmd) ; |
101 |
+#endif |
101 |
+#endif |
102 |
|
102 |
|
103 |
// set broad firewall rule |
103 |
// set broad firewall rule |
104 |
sprintf (G_fw_broad_rule, " %s %s 0 %s %s", |
104 |
sprintf (G_fw_broad_rule, " %s %s 0 %s %s", |
105 |
@@ -659,7 +722,22 @@ |
105 |
@@ -716,7 +779,22 @@ |
106 |
|
106 |
|
107 |
for (;;) { |
107 |
for (;;) { |
108 |
|
108 |
|
109 |
+#ifdef __FreeBSD__ |
109 |
+#ifdef __FreeBSD__ |
110 |
+ { |
110 |
+ { |
111 |
+ int ret = 0; |
111 |
+ int ret = 0; |
112 |
+ struct pcap_pkthdr * packet_hdr_p; |
112 |
+ struct pcap_pkthdr * packet_hdr_p; |
113 |
+ |
113 |
+ |
114 |
+ while (ret == 0) { |
114 |
+ while (ret == 0) { |
115 |
+ ret = pcap_next_ex (cap, &packet_hdr_p, (const u_char **)&p) ; |
115 |
+ ret = pcap_next_ex (cap, &packet_hdr_p, (const u_char **)&p) ; |
116 |
+ packet_hdr = *packet_hdr_p; |
116 |
+ packet_hdr = *packet_hdr_p; |
117 |
+ if (ret < 0) { |
117 |
+ if (ret < 0) { |
118 |
+ p = NULL; |
118 |
+ p = NULL; |
119 |
+ } |
119 |
+ } |
120 |
+ } |
120 |
+ } |
121 |
+ } |
121 |
+ } |
122 |
+#else |
122 |
+#else |
123 |
p = (unsigned char*)pcap_next (cap, &packet_hdr) ; |
123 |
p = (unsigned char*)pcap_next (cap, &packet_hdr) ; |
124 |
+#endif |
124 |
+#endif |
125 |
if (p == NULL) { |
125 |
if (p == NULL) { |
126 |
WARNX "manage_firewall got null from 'pcap_next'. Exiting.") ; |
126 |
WARNX "manage_firewall got null from 'pcap_next': %s Exiting.", |
127 |
exit (1) ; |
127 |
pcap_geterr(G_cap)) ; |
128 |
@@ -1222,9 +1300,13 @@ |
128 |
@@ -1300,9 +1378,13 @@ |
129 |
croak (errno, "Can't get interface address of %s", device) ; |
129 |
croak (errno, "Can't get interface address of %s", device) ; |
130 |
} |
130 |
} |
131 |
|
131 |
|
132 |
+#ifdef __FreeBSD__ |
132 |
+#ifdef __FreeBSD__ |
133 |
+ hdr_len = open_a_pcap (device, 1000, &G_cap, "udp and port %d and dst %s", |
133 |
+ hdr_len = open_a_pcap (device, 1000, &G_cap, "udp and port %d and dst %s", |
134 |
+ port, interface_ip) ; |
134 |
+ port, interface_ip) ; |
135 |
+#else |
135 |
+#else |
136 |
hdr_len = open_a_pcap (device, 0, &G_cap, "udp and port %d and dst %s", |
136 |
hdr_len = open_a_pcap (device, 0, &G_cap, "udp and port %d and dst %s", |
137 |
port, interface_ip) ; |
137 |
port, interface_ip) ; |
138 |
- |
138 |
- |
139 |
+#endif |
139 |
+#endif |
140 |
if (G_reconfigure) { |
140 |
if (G_reconfigure) { |
141 |
G_reconfigure = FALSE ; |
141 |
G_reconfigure = FALSE ; |
142 |
NOTICE "reconfigured.") ; |
142 |
NOTICE "reconfigured.") ; |
143 |
@@ -1252,7 +1334,22 @@ |
143 |
@@ -1330,9 +1412,22 @@ |
144 |
char src_addr_buff[16] ; |
144 |
char src_addr_buff[16] ; |
145 |
|
145 |
|
146 |
errno = 0 ; |
146 |
errno = 0 ; |
147 |
+#ifdef __FreeBSD__ |
147 |
- netdown_count = 0 ; |
148 |
+ { |
148 |
- |
149 |
+ int ret = 0; |
149 |
+#ifdef __FreeBSD__ |
150 |
+ struct pcap_pkthdr * packet_hdr_p; |
150 |
+ { |
151 |
+ |
151 |
+ int ret = 0; |
152 |
+ while (ret == 0) { |
152 |
+ struct pcap_pkthdr * packet_hdr_p; |
153 |
+ ret = pcap_next_ex (G_cap, &packet_hdr_p, (const u_char **)&p) ; |
153 |
+ |
154 |
+ packet_hdr = *packet_hdr_p; |
154 |
+ while (ret == 0) { |
155 |
+ if (ret < 0) { |
155 |
+ ret = pcap_next_ex (G_cap, &packet_hdr_p, (const u_char **)&p) ; |
156 |
+ p = NULL; |
156 |
+ packet_hdr = *packet_hdr_p; |
157 |
+ } |
157 |
+ if (ret < 0) { |
158 |
+ } |
158 |
+ p = NULL; |
159 |
+ } |
159 |
+ } |
160 |
+#else |
160 |
+ } |
161 |
p = (unsigned char *)pcap_next (G_cap, &packet_hdr) ; |
161 |
+ } |
162 |
+#endif |
162 |
+#else |
163 |
if (G_reconfigure) { |
163 |
p = (unsigned char *)pcap_next (G_cap, &packet_hdr) ; |
164 |
if (daemonize) err_closelog() ; |
164 |
+#endif |
165 |
goto reconfigure ; |
165 |
if (G_reconfigure) { |
|
|
166 |
if (daemonize) err_closelog() ; |
167 |
goto reconfigure ; |