forked from Yellow-Camper/libevhtp
-
Notifications
You must be signed in to change notification settings - Fork 17
/
log.c
300 lines (246 loc) · 7.49 KB
/
log.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <assert.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <sys/queue.h>
#include "evhtp/evhtp.h"
#include "evhtp/log.h"
typedef enum {
HTP_LOG_OP_USERAGENT = 1,
HTP_LOG_OP_PATH,
HTP_LOG_OP_RHOST,
HTP_LOG_OP_METHOD,
HTP_LOG_OP_TIMESTAMP,
HTP_LOG_OP_PROTO,
HTP_LOG_OP_STATUS,
HTP_LOG_OP_REFERRER,
HTP_LOG_OP_HOST,
HTP_LOG_OP_HEADER,
HTP_LOG_OP_PRINTABLE
} htp_log_op_type;
struct {
char * fmt_;
htp_log_op_type type_;
} op_type_strmap_[] = {
{ "$ua", HTP_LOG_OP_USERAGENT },
{ "$path", HTP_LOG_OP_PATH },
{ "$rhost", HTP_LOG_OP_RHOST },
{ "$meth", HTP_LOG_OP_METHOD },
{ "$ts", HTP_LOG_OP_TIMESTAMP },
{ "$proto", HTP_LOG_OP_PROTO },
{ "$status", HTP_LOG_OP_STATUS },
{ "$ref", HTP_LOG_OP_REFERRER },
{ "$host", HTP_LOG_OP_HOST },
{ "$hdr::", HTP_LOG_OP_HEADER },
{ NULL, HTP_LOG_OP_PRINTABLE }
};
#define HTP_LOG_OP_TAGSZ 1024
struct htp_log_op {
htp_log_op_type type;
size_t len;
char tag[HTP_LOG_OP_TAGSZ];
TAILQ_ENTRY(htp_log_op) next;
};
TAILQ_HEAD(htp_log_format, htp_log_op);
htp_log_op_type
htp_log_str_to_op_type_(const char * fmt, int * arglen)
{
int i;
for (i = 0; op_type_strmap_[i].fmt_; i++) {
const char * fmt_ = op_type_strmap_[i].fmt_;
htp_log_op_type type_ = op_type_strmap_[i].type_;
if (!strncasecmp(fmt_, fmt, strlen(fmt_))) {
*arglen = strlen(fmt_);
return type_;
}
}
return 0;
}
static struct htp_log_format *
htp_log_format_new_(void)
{
struct htp_log_format * format;
format = calloc(1, sizeof(*format));
TAILQ_INIT(format);
return format;
}
static struct htp_log_op *
htp_log_op_new_(htp_log_op_type type)
{
struct htp_log_op * op;
op = calloc(1, sizeof(*op));
op->type = type;
return op;
}
/**
* @brief [debug] print the psuedo-stack
*
* @param stack
*/
static void
dump_(struct htp_log_format * format)
{
struct htp_log_op * op;
TAILQ_FOREACH(op, format, next) {
switch (op->type) {
case HTP_LOG_OP_USERAGENT:
printf("$ua");
break;
case HTP_LOG_OP_PATH:
printf("$path");
break;
case HTP_LOG_OP_METHOD:
printf("$meth");
break;
case HTP_LOG_OP_TIMESTAMP:
printf("$ts");
break;
case HTP_LOG_OP_REFERRER:
printf("$ref");
break;
case HTP_LOG_OP_RHOST:
printf("$rhost");
break;
case HTP_LOG_OP_STATUS:
printf("$status");
break;
case HTP_LOG_OP_HOST:
printf("$host");
break;
case HTP_LOG_OP_HEADER:
printf("$hdr::");
break;
case HTP_LOG_OP_PROTO:
printf("$proto");
break;
case HTP_LOG_OP_PRINTABLE:
printf("%s", op->tag);
break;
} /* switch */
}
printf("\n");
} /* log_format_dump_ */
static void
htp_log_format_addchar_(struct htp_log_format * format, const char c)
{
struct htp_log_op * op;
if (TAILQ_EMPTY(format)) {
/* the format op stack is empty, we want to allocate a new one */
op = NULL;
} else {
/* reuse the last format op, appending the character */
op = TAILQ_LAST(format, htp_log_format);
}
if (op == NULL || op->type != HTP_LOG_OP_PRINTABLE) {
/* the last op in the format stack was not a type of PRINTABLE or
* NULL, so allocate a new one.
*/
op = htp_log_op_new_(HTP_LOG_OP_PRINTABLE);
/* insert the newly allocated stack_ent into the stack */
TAILQ_INSERT_TAIL(format, op, next);
}
/* append the character to the stack_ent */
op->tag[op->len++] = c;
op->tag[op->len] = '\0';
}
#define IS_FMTVAR(X) (*X == '$' && *(X + 1) != '$')
/**
* @brief using an input string, create a stack of information that will be
* logged.
*
* @param fmt
*
* @return
*/
static struct htp_log_format *
htp_log_format_compile_(const char * strfmt)
{
const char * strp;
struct htp_log_format * format;
format = htp_log_format_new_();
for (strp = strfmt; *strp != '\0'; strp++) {
struct htp_log_op * op;
/* if the character is a format variable ($var),
* then create a new stack entry, and insert it
* into the stack. Otherwise append the character
* to the last stack entry
*/
if (IS_FMTVAR(strp)) {
int arglen;
op = htp_log_op_new_(htp_log_str_to_op_type_(strp, &arglen));
strp += arglen - 1;
TAILQ_INSERT_TAIL(format, op, next);
} else {
htp_log_format_addchar_(format, *strp);
}
}
return format;
}
void *
evhtp_log_new(const char * fmtstr)
{
return htp_log_format_compile_(fmtstr);
}
void
evhtp_log_request_f(void * format_p, evhtp_request_t * request, FILE * fp)
{
struct htp_log_format * format = format_p;
struct htp_log_op * op;
struct timeval tv;
struct tm * tm;
struct sockaddr_in * sin;
char tmp[64];
TAILQ_FOREACH(op, format, next) {
const char * logstr = NULL;
switch (op->type) {
case HTP_LOG_OP_USERAGENT:
logstr = evhtp_header_find(request->headers_in, "user-agent");
break;
case HTP_LOG_OP_PATH:
if (request->uri && request->uri->path && request->uri->path->full) {
logstr = request->uri->path->full;
}
break;
case HTP_LOG_OP_METHOD:
if (request->conn->parser) {
logstr = htparser_get_methodstr(request->conn->parser);
}
break;
case HTP_LOG_OP_TIMESTAMP:
event_base_gettimeofday_cached(request->conn->evbase, &tv);
tm = localtime(&tv.tv_sec);
strftime(tmp, sizeof(tmp), "%d/%b/%Y:%X %z", tm);
logstr = tmp;
break;
case HTP_LOG_OP_REFERRER:
logstr = evhtp_header_find(request->headers_in, "referer");
break;
case HTP_LOG_OP_RHOST:
sin = (struct sockaddr_in *)request->conn->saddr;
evutil_inet_ntop(AF_INET, &sin->sin_addr, tmp, sizeof(tmp));
logstr = tmp;
break;
case HTP_LOG_OP_STATUS:
fprintf(fp, "%d", evhtp_request_status(request));
continue;
case HTP_LOG_OP_HOST:
logstr =
request->htp->server_name ? : evhtp_header_find(request->headers_in, "host");
break;
case HTP_LOG_OP_PROTO:
logstr = evhtp_request_get_proto(request) == EVHTP_PROTO_11 ? "1.1" : "1.0";
break;
case HTP_LOG_OP_HEADER:
/* not implemented yet - fallthrough */
case HTP_LOG_OP_PRINTABLE:
logstr = op->tag;
break;
} /* switch */
fputs(logstr ? : "-", fp);
}
fputc('\n', fp);
} /* evhtp_log_request_f */