-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod_visitor.c
303 lines (247 loc) · 6.93 KB
/
mod_visitor.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
#include "apr.h"
#include "apr_strings.h"
#define APR_WANT_STRFUNC
#include "apr_want.h"
#include "httpd.h"
#include "http_config.h"
#include "http_core.h"
#include "http_log.h"
#include "http_request.h"
module AP_MODULE_DECLARE_DATA visitor_module;
typedef struct {
int enabled;
char *domain;
apr_int64_t vexpiry;
apr_int64_t sexpiry;
} visitor_dir_rec;
static uint8_t visitor_image[] = {
// GIF header
0x47, 0x49, 0x46,
// version (89a)
0x38, 0x39, 0x61,
// image size (1x1 px)
0x01, 0x00, 0x01, 0x00,
// flags + color index
0x90, 0x00, 0x00,
// color palette
0xff, 0xff, 0xff,
// image block
0x00, 0x00, 0x00,
0x2c, 0x00, 0x00,
0x00, 0x00, 0x01,
0x00, 0x01, 0x00,
0x00, 0x02, 0x02,
0x04, 0x01, 0x00,
// trailer
0x3b
};
static
void set_cookie(request_rec *r,
const char *name, const char *val,
apr_time_t expiry)
{
/* calculate expiry time in GMT */
apr_time_exp_t tms;
apr_time_exp_gmt(&tms,
r->request_time + apr_time_from_sec(expiry));
/* generate cookie header */
char *new_cookie;
new_cookie = apr_psprintf(r->pool,
"%s=%s; path=/",
name, val);
if (expiry > 0) {
new_cookie = apr_psprintf(r->pool,
"%s; expires=%s, "
"%.2d-%s-%.2d %.2d:%.2d:%.2d GMT",
new_cookie, apr_day_snames[tms.tm_wday],
tms.tm_mday, apr_month_snames[tms.tm_mon],
tms.tm_year % 100,
tms.tm_hour, tms.tm_min, tms.tm_sec);
}
/* append domain name if configured */
visitor_dir_rec *dcfg = ap_get_module_config(r->per_dir_config, &visitor_module);
if (dcfg->domain != NULL) {
new_cookie = apr_pstrcat(r->pool,
new_cookie, "; domain=",
dcfg->domain, NULL);
}
/* add cookie header */
apr_table_addn(r->headers_out, "Set-Cookie", new_cookie);
}
static
const char *get_cookie(request_rec *r, const char *name)
{
const char *cookies;
cookies = apr_table_get(r->headers_in, "Cookie");
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
"searching cookie '%s' in '%s'", name, cookies);
if (cookies == NULL)
return NULL;
const char *re_str = apr_pstrcat(r->pool,
"^", name, "=([^;,]+)|[;,][ \t]*", name, "=([^;,]+)", NULL);
ap_regex_t *re_bin = ap_pregcomp(r->pool, re_str, AP_REG_EXTENDED);
ap_assert(re_bin != NULL);
char *val = NULL;
ap_regmatch_t regm[3];
if (!ap_regexec(re_bin, cookies, 3, regm, 0)) {
if (regm[1].rm_so != -1)
val = apr_pstrndup(r->pool,
cookies + regm[1].rm_so,
regm[1].rm_eo - regm[1].rm_so);
if (regm[2].rm_so != -1)
val = apr_pstrndup(r->pool,
cookies + regm[2].rm_so,
regm[2].rm_eo - regm[2].rm_so);
}
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
"found cookie: '%s'", val);
return val;
}
static
const char *make_cookie(request_rec *r, const char *name, apr_time_t expiry)
{
const char *id;
if ((id = apr_table_get(r->subprocess_env, "UNIQUE_ID")))
set_cookie(r, name, id, expiry);
return id;
}
static
void spot_cookies(request_rec *r)
{
visitor_dir_rec *dcfg = ap_get_module_config(r->per_dir_config,
&visitor_module);
/* do not run in subrequests */
if (r->main)
return;
const char *a = get_cookie(r, "__vta");
const char *b = get_cookie(r, "__vtb");
const char *c = get_cookie(r, "__vtc");
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
"got visitor cookies: a=%s; b=%s; c=%s", a, b, c);
if (a == NULL) {
a = make_cookie(r, "__vta", dcfg->vexpiry);
b = make_cookie(r, "__vtb", dcfg->sexpiry);
c = make_cookie(r, "__vtc", 0);
} else if (b == NULL || c == NULL) {
b = make_cookie(r, "__vtb", dcfg->sexpiry);
c = make_cookie(r, "__vtc", 0);
}
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
"new visitor cookies: a=%s; b=%s; c=%s", a, b, c);
if (a && b)
apr_table_setn(r->notes, "visitor-cookie",
apr_pstrcat(r->pool, a, ";", b, NULL));
return;
}
static
int visitor_cookie_handler(request_rec *r)
{
if (strcmp(r->handler, "visitor-cookie"))
return DECLINED;
r->allowed |= (AP_METHOD_BIT << M_GET);
if (r->method_number != M_GET)
return DECLINED;
/* check and set cookies */
spot_cookies(r);
/* send images */
ap_set_content_type(r, "image/gif");
ap_set_content_length(r, sizeof(visitor_image));
ap_rwrite(visitor_image, sizeof(visitor_image), r);
return OK;
}
static void *visitor_create_dir_config(apr_pool_t *p, char *d)
{
visitor_dir_rec *dcfg = apr_pcalloc(p, sizeof(visitor_dir_rec));
dcfg->enabled = 0;
dcfg->domain = NULL;
dcfg->vexpiry = 2*365*24*60*60; /* aprox. 2 years */
dcfg->sexpiry = 30*60; /* 30 minutes */
return dcfg;
}
static const char *cmd_visitor_tracking(cmd_parms *cmd, void *mconfig, int arg)
{
visitor_dir_rec *dcfg = mconfig;
dcfg->enabled = arg;
return NULL;
}
static
const char *cmd_visitor_domain(cmd_parms *cmd, void *mconfig, const char *name)
{
visitor_dir_rec *dcfg = mconfig;
if (strlen(name) == 0)
return "VisitorDomain values may not be null";
if (name[0] != '.')
return "VisitorDomain values must begin with a dot";
if (ap_strchr_c(&name[1], '.') == NULL)
return "VisitorDomain values must contain at least one embedded dot";
dcfg->domain = apr_pstrdup(cmd->pool, name);
return NULL;
}
static
const char *cmd_visitor_expiry(cmd_parms *cmd, void *mconfig, const char *seconds)
{
visitor_dir_rec *dcfg = mconfig;
if (strlen(seconds) == 0)
return "VisitorExpiry values may not be null";
char *end;
apr_int64_t expiry = apr_strtoi64(seconds, &end, 10);
if (end - seconds < strlen(seconds) || expiry < 0)
return "VisitorExpiry value is not a positive number";
dcfg->vexpiry = expiry;
return NULL;
}
static
const char *cmd_session_expiry(cmd_parms *cmd, void *mconfig, const char *seconds)
{
visitor_dir_rec *dcfg = mconfig;
if (strlen(seconds) == 0)
return "SessionExpiry values may not be null";
char *end;
apr_int64_t expiry = apr_strtoi64(seconds, &end, 10);
if (end - seconds < strlen(seconds) || expiry < 0)
return "SessionExpiry value is not a positive number";
dcfg->sexpiry = expiry;
return NULL;
}
static
const command_rec visitor_cmds[] = {
AP_INIT_FLAG(
"VisitorTracking",
cmd_visitor_tracking,
NULL,
OR_FILEINFO,
"whether or not to enable visitor tracking"),
AP_INIT_TAKE1(
"VisitorDomain",
cmd_visitor_domain,
NULL,
OR_FILEINFO,
"domain to which mod_visitor cookies apply"),
AP_INIT_TAKE1(
"VisitorExpiry",
cmd_visitor_expiry,
NULL,
OR_FILEINFO,
"time after which visitor cookie expires in seconds"),
AP_INIT_TAKE1(
"SessionExpiry",
cmd_session_expiry,
NULL,
OR_FILEINFO,
"time after which session cookie expires in seconds"),
{ NULL }
};
static
void visitor_register_hooks(apr_pool_t *p)
{
ap_hook_handler(visitor_cookie_handler, NULL, NULL, APR_HOOK_FIRST);
}
module AP_MODULE_DECLARE_DATA visitor_module = {
STANDARD20_MODULE_STUFF,
visitor_create_dir_config, /* per-directory config creater */
NULL, /* dir config merger */
NULL, /* server config creator */
NULL, /* server config merger */
visitor_cmds, /* command table */
visitor_register_hooks /* set up other request processing hooks */
};