-
Notifications
You must be signed in to change notification settings - Fork 1
/
p0f.c
317 lines (270 loc) · 7.21 KB
/
p0f.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
/* $Id: p0f.c,v 1.8 2009/02/21 22:54:32 manu Exp $ */
/*
* Copyright (c) 2008 Emmanuel Dreyfus
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Emmanuel Dreyfus
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#ifdef USE_P0F
#ifdef HAVE_SYS_CDEFS_H
#include <sys/cdefs.h>
#ifdef __RCSID
__RCSID("$Id: p0f.c,v 1.8 2009/02/21 22:54:32 manu Exp $");
#endif
#endif
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#include <arpa/inet.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <errno.h>
#include <err.h>
#include <sysexits.h>
#include <syslog.h>
#include "conf.h"
#include "spf.h"
#include "acl.h"
#include "milter-greylist.h"
#include "p0f.h"
#ifdef P0F_QUERY_FROM_P0F_DIST
#include <p0f-query.h>
#else /* P0F_QUERY_FROM_P0F_DIST */
/* This is from p0f/p0f-query.h */
#define QUERY_MAGIC 0x0defaced
#define QTYPE_FINGERPRINT 1
#define RESP_BADQUERY 1
#define RESP_NOMATCH 2
struct p0f_query {
u_int32_t magic;
u_int8_t type;
u_int32_t id;
u_int32_t src_ad,dst_ad;
u_int16_t src_port,dst_port;
};
struct p0f_response {
u_int32_t magic;
u_int32_t id;
u_int8_t type;
u_int8_t genre[20];
u_int8_t detail[40];
int8_t dist;
u_int8_t link[30];
u_int8_t tos[30];
u_int8_t fw,nat;
u_int8_t real;
int16_t score;
u_int16_t mflags;
int32_t uptime;
};
/* End of stuff borrowed from p0f/p0f-query.h */
#endif /* P0F_QUERY_FROM_P0F_DIST */
static int p0f_reconnect(void);
static int p0fsock = -1;
void
p0f_init(void)
{
return;
}
void
p0f_clear(void)
{
if (p0fsock != -1) {
(void)shutdown(p0fsock, SHUT_RDWR);
(void)close(p0fsock);
p0fsock = -1;
}
p0f_init();
return;
}
int
p0f_cmp(ad, stage, ap, priv)
acl_data_t *ad;
acl_stage_t stage;
struct acl_param *ap;
struct mlfi_priv *priv;
{
char *data;
if (priv->priv_p0f == NULL)
return 0;
data = (char *)ad->string;
if (strcasestr(priv->priv_p0f, data) != NULL)
return 1;
return 0;
}
int
p0f_regexec(ad, stage, ap, priv)
acl_data_t *ad;
acl_stage_t stage;
struct acl_param *ap;
struct mlfi_priv *priv;
{
if (priv->priv_p0f == NULL)
return 0;
if (myregexec(priv, ad, ap, priv->priv_helo) == 0)
return 1;
return 0;
}
int
p0f_lookup(priv)
struct mlfi_priv *priv;
{
struct p0f_query req;
struct p0f_response rep;
struct timeval tv;
char *daddr;
char *dport;
size_t len;
char sastr[IPADDRSTRLEN + 1];
char dastr[IPADDRSTRLEN + 1];
char dpstr[IPADDRSTRLEN + 1];
/*
* The p0f query interface semms to only support IPv4
*/
if (SA(&priv->priv_addr)->sa_family != AF_INET)
return -1;
if ((daddr = smfi_getsymval(priv->priv_ctx, "{if_addr}")) == NULL) {
mg_log(LOG_DEBUG, "smfi_getsymval failed for {if_addr}");
return -1;
}
if ((dport = smfi_getsymval(priv->priv_ctx, "{daemon_port}")) == NULL) {
struct servent *s;
mg_log(LOG_WARNING, "smfi_getsymval failed for {daemon_port}, "
"using default smtp port");
if ((s = getservbyname("smtp", "tcp")) == NULL) {
mg_log(LOG_ERR,
"getservbyname(\"smtp\", \"tcp\") failed");
exit (EX_OSFILE);
}
(void)snprintf(dpstr, sizeof(dpstr), "%d", s->s_port);
dport = dpstr;
}
if (p0f_reconnect() != 0)
return -1;
memset(&req, 0, sizeof(req));
memset(&rep, 0, sizeof(rep));
(void)gettimeofday(&tv, NULL);
req.magic = QUERY_MAGIC;
req.id = tv.tv_usec;
req.type = QTYPE_FINGERPRINT;
req.src_ad = SADDR4(&priv->priv_addr)->s_addr;
req.src_port = htons(SA4(&priv->priv_addr)->sin_port);
req.dst_ad = inet_addr(daddr);
req.dst_port = atoi(dport);
if (conf.c_debug)
mg_log(LOG_DEBUG, "p0f_lookup: %s[%d] -> %s[%d]",
inet_ntop(AF_INET, &req.src_ad, sastr, IPADDRSTRLEN),
req.src_port,
inet_ntop(AF_INET, &req.dst_ad, dastr, IPADDRSTRLEN),
req.dst_port);
if (write(p0fsock, &req ,sizeof(req)) != sizeof(req)) {
mg_log(LOG_ERR, "writing to \"%s\" failed", conf.c_p0fsock);
p0f_clear();
return -1;
}
if (read(p0fsock, &rep, sizeof(rep)) != sizeof(rep)) {
mg_log(LOG_ERR, "reading from \"%s\" failed", conf.c_p0fsock);
p0f_clear();
return -1;
}
p0f_clear();
if (rep.id != req.id) {
mg_log(LOG_ERR, "p0f reply id mismatch %x expected %x",
rep.id, req.id);
return -1;
}
if (rep.magic != QUERY_MAGIC) {
mg_log(LOG_ERR, "Unexpected p0f magic = %d", rep.magic);
return -1;
}
switch(rep.type) {
case RESP_BADQUERY:
mg_log(LOG_INFO, "p0f rejected query");
return -1;
break;
case RESP_NOMATCH:
mg_log(LOG_INFO, "p0f cache miss");
return -1; /* XXX This causes a tempfail */
break;
default:
break;
}
/* +2 for space and trailing \0 */
len = strlen((char *)rep.genre) + strlen((char *)rep.detail) + 2;
if ((priv->priv_p0f = malloc(len)) == NULL) {
mg_log(LOG_ERR, "malloc(%d) failed: %s", len, strerror(errno));
exit(EX_OSERR);
}
(void)sprintf(priv->priv_p0f, "%s %s", rep.genre, rep.detail);
if (conf.c_debug)
mg_log(LOG_DEBUG, "p0f identified \"%s\"", priv->priv_p0f);
return 0;
}
void
p0f_sock_set(sock)
char *sock;
{
(void)strncpy(conf.c_p0fsock, sock, sizeof(conf.c_p0fsock));
return;
}
static int
p0f_reconnect(void)
{
struct sockaddr_un sun;
if (p0fsock != -1)
return 0;
if (!conf.c_p0fsock[0])
return -1;
if ((p0fsock = socket(PF_UNIX,SOCK_STREAM,0)) == -1) {
mg_log(LOG_ERR, "socket(PF_UNIX, SOCK_STREAM, 0) failed");
exit(EX_OSERR);
}
if (p0fsock == -1) {
mg_log(LOG_ERR, "p0f socket not initialized");
exit(EX_SOFTWARE);
}
if (conf.c_debug)
mg_log(LOG_DEBUG, "using p0f socket \"%s\"", conf.c_p0fsock);
(void)memset(&sun, 0, sizeof(sun));
sun.sun_family = AF_UNIX;
strncpy(sun.sun_path, conf.c_p0fsock, sizeof(sun.sun_path));
if (connect(p0fsock, (struct sockaddr *)&sun, sizeof(sun)) != 0) {
mg_log(LOG_ERR, "Cannot connect to p0f socket \"%s\"",
conf.c_p0fsock);
close(p0fsock);
p0fsock = -1;
return -1;
}
return 0;
}
#endif /* USE_P0F */