0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_http.h>
|
|
10
|
|
11
|
|
12 #define NGX_HTTP_USERID_OFF 0
|
|
13 #define NGX_HTTP_USERID_LOG 1
|
|
14 #define NGX_HTTP_USERID_V1 2
|
|
15 #define NGX_HTTP_USERID_ON 3
|
|
16
|
|
17 /* 31 Dec 2037 23:55:55 GMT */
|
|
18 #define NGX_HTTP_USERID_MAX_EXPIRES 2145916555
|
|
19
|
|
20
|
|
21 typedef struct {
|
|
22 ngx_flag_t enable;
|
|
23
|
|
24 ngx_int_t service;
|
|
25
|
|
26 ngx_str_t name;
|
|
27 ngx_str_t domain;
|
|
28 ngx_str_t path;
|
|
29 time_t expires;
|
10
|
30 ngx_str_t p3p;
|
0
|
31 } ngx_http_userid_conf_t;
|
|
32
|
|
33
|
|
34 typedef struct {
|
10
|
35 uint32_t uid_got[4];
|
|
36 uint32_t uid_set[4];
|
0
|
37 } ngx_http_userid_ctx_t;
|
|
38
|
|
39
|
|
40 static ngx_int_t ngx_http_userid_get_uid(ngx_http_request_t *r,
|
|
41 ngx_http_userid_ctx_t *ctx,
|
|
42 ngx_http_userid_conf_t *conf);
|
|
43 static ngx_int_t ngx_http_userid_set_uid(ngx_http_request_t *r,
|
|
44 ngx_http_userid_ctx_t *ctx,
|
|
45 ngx_http_userid_conf_t *conf);
|
|
46
|
28
|
47 static size_t ngx_http_userid_log_uid_got_getlen(ngx_http_request_t *r,
|
|
48 uintptr_t data);
|
0
|
49 static u_char *ngx_http_userid_log_uid_got(ngx_http_request_t *r, u_char *buf,
|
28
|
50 ngx_http_log_op_t *op);
|
|
51 static size_t ngx_http_userid_log_uid_set_getlen(ngx_http_request_t *r,
|
|
52 uintptr_t data);
|
0
|
53 static u_char *ngx_http_userid_log_uid_set(ngx_http_request_t *r, u_char *buf,
|
28
|
54 ngx_http_log_op_t *op);
|
0
|
55
|
28
|
56 static ngx_int_t ngx_http_userid_add_log_formats(ngx_conf_t *cf);
|
0
|
57 static ngx_int_t ngx_http_userid_init(ngx_cycle_t *cycle);
|
|
58 static void *ngx_http_userid_create_conf(ngx_conf_t *cf);
|
|
59 static char *ngx_http_userid_merge_conf(ngx_conf_t *cf, void *parent,
|
|
60 void *child);
|
10
|
61 char *ngx_http_userid_domain(ngx_conf_t *cf, void *post, void *data);
|
|
62 char *ngx_http_userid_path(ngx_conf_t *cf, void *post, void *data);
|
0
|
63 char *ngx_http_userid_expires(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
|
10
|
64 char *ngx_http_userid_p3p(ngx_conf_t *cf, void *post, void *data);
|
0
|
65
|
|
66
|
|
67 static uint32_t sequencer_v1 = 1;
|
|
68 static uint32_t sequencer_v2 = 0x03030302;
|
|
69
|
|
70
|
|
71 static u_char expires[] = "; expires=Thu, 31-Dec-37 23:55:55 GMT";
|
|
72
|
|
73
|
|
74 static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
|
|
75
|
|
76
|
|
77 static ngx_conf_enum_t ngx_http_userid_state[] = {
|
|
78 { ngx_string("off"), NGX_HTTP_USERID_OFF },
|
|
79 { ngx_string("log"), NGX_HTTP_USERID_LOG },
|
|
80 { ngx_string("v1"), NGX_HTTP_USERID_V1 },
|
|
81 { ngx_string("on"), NGX_HTTP_USERID_ON },
|
|
82 { ngx_null_string, 0 }
|
|
83 };
|
|
84
|
|
85
|
10
|
86 static ngx_conf_post_handler_pt ngx_http_userid_domain_p =
|
|
87 ngx_http_userid_domain;
|
|
88
|
|
89 static ngx_conf_post_handler_pt ngx_http_userid_path_p = ngx_http_userid_path;
|
|
90 static ngx_conf_post_handler_pt ngx_http_userid_p3p_p = ngx_http_userid_p3p;
|
0
|
91
|
|
92
|
|
93 static ngx_command_t ngx_http_userid_commands[] = {
|
|
94
|
|
95 { ngx_string("userid"),
|
|
96 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
97 ngx_conf_set_enum_slot,
|
|
98 NGX_HTTP_LOC_CONF_OFFSET,
|
|
99 offsetof(ngx_http_userid_conf_t, enable),
|
|
100 ngx_http_userid_state },
|
|
101
|
|
102 { ngx_string("userid_service"),
|
|
103 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
104 ngx_conf_set_num_slot,
|
|
105 NGX_HTTP_LOC_CONF_OFFSET,
|
|
106 offsetof(ngx_http_userid_conf_t, service),
|
|
107 NULL },
|
|
108
|
|
109 { ngx_string("userid_name"),
|
|
110 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
111 ngx_conf_set_str_slot,
|
|
112 NGX_HTTP_LOC_CONF_OFFSET,
|
|
113 offsetof(ngx_http_userid_conf_t, name),
|
|
114 NULL },
|
|
115
|
|
116 { ngx_string("userid_domain"),
|
|
117 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
118 ngx_conf_set_str_slot,
|
|
119 NGX_HTTP_LOC_CONF_OFFSET,
|
|
120 offsetof(ngx_http_userid_conf_t, domain),
|
10
|
121 &ngx_http_userid_domain_p },
|
0
|
122
|
|
123 { ngx_string("userid_path"),
|
|
124 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
125 ngx_conf_set_str_slot,
|
|
126 NGX_HTTP_LOC_CONF_OFFSET,
|
|
127 offsetof(ngx_http_userid_conf_t, path),
|
10
|
128 &ngx_http_userid_path_p },
|
0
|
129
|
|
130 { ngx_string("userid_expires"),
|
|
131 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
132 ngx_http_userid_expires,
|
|
133 NGX_HTTP_LOC_CONF_OFFSET,
|
|
134 0,
|
|
135 NULL },
|
|
136
|
10
|
137 { ngx_string("userid_p3p"),
|
|
138 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
139 ngx_conf_set_str_slot,
|
|
140 NGX_HTTP_LOC_CONF_OFFSET,
|
|
141 offsetof(ngx_http_userid_conf_t, p3p),
|
|
142 &ngx_http_userid_p3p_p },
|
|
143
|
0
|
144 ngx_null_command
|
|
145 };
|
|
146
|
|
147
|
|
148 ngx_http_module_t ngx_http_userid_filter_module_ctx = {
|
28
|
149 ngx_http_userid_add_log_formats, /* pre conf */
|
0
|
150
|
|
151 NULL, /* create main configuration */
|
|
152 NULL, /* init main configuration */
|
|
153
|
|
154 NULL, /* create server configuration */
|
|
155 NULL, /* merge server configuration */
|
|
156
|
|
157 ngx_http_userid_create_conf, /* create location configration */
|
|
158 ngx_http_userid_merge_conf /* merge location configration */
|
|
159 };
|
|
160
|
|
161
|
|
162 ngx_module_t ngx_http_userid_filter_module = {
|
|
163 NGX_MODULE,
|
|
164 &ngx_http_userid_filter_module_ctx, /* module context */
|
|
165 ngx_http_userid_commands, /* module directives */
|
|
166 NGX_HTTP_MODULE, /* module type */
|
|
167 ngx_http_userid_init, /* init module */
|
|
168 NULL /* init process */
|
|
169 };
|
|
170
|
|
171
|
|
172 static ngx_http_log_op_name_t ngx_http_userid_log_fmt_ops[] = {
|
28
|
173 { ngx_string("uid_got"), 0, NULL,
|
|
174 ngx_http_userid_log_uid_got_getlen,
|
|
175 ngx_http_userid_log_uid_got },
|
|
176 { ngx_string("uid_set"), 0, NULL,
|
|
177 ngx_http_userid_log_uid_set_getlen,
|
|
178 ngx_http_userid_log_uid_set },
|
|
179 { ngx_null_string, 0, NULL, NULL, NULL }
|
0
|
180 };
|
|
181
|
|
182
|
|
183 static ngx_int_t ngx_http_userid_filter(ngx_http_request_t *r)
|
|
184 {
|
|
185 ngx_int_t rc;
|
|
186 ngx_http_userid_ctx_t *ctx;
|
|
187 ngx_http_userid_conf_t *conf;
|
|
188
|
|
189 conf = ngx_http_get_module_loc_conf(r, ngx_http_userid_filter_module);
|
|
190
|
|
191 if (conf->enable == NGX_HTTP_USERID_OFF) {
|
|
192 return ngx_http_next_header_filter(r);
|
|
193 }
|
|
194
|
|
195 ngx_http_create_ctx(r, ctx, ngx_http_userid_filter_module,
|
|
196 sizeof(ngx_http_userid_ctx_t), NGX_ERROR);
|
|
197
|
|
198 rc = ngx_http_userid_get_uid(r, ctx, conf);
|
|
199
|
|
200 if (rc != NGX_OK) {
|
|
201 return rc;
|
|
202 }
|
|
203
|
|
204 if (conf->enable == NGX_HTTP_USERID_LOG || ctx->uid_got[3] != 0) {
|
|
205 return ngx_http_next_header_filter(r);
|
|
206 }
|
|
207
|
|
208 rc = ngx_http_userid_set_uid(r, ctx, conf);
|
|
209
|
|
210 if (rc != NGX_OK) {
|
|
211 return rc;
|
|
212 }
|
|
213
|
|
214 return ngx_http_next_header_filter(r);
|
|
215 }
|
|
216
|
|
217
|
|
218 static ngx_int_t ngx_http_userid_get_uid(ngx_http_request_t *r,
|
|
219 ngx_http_userid_ctx_t *ctx,
|
|
220 ngx_http_userid_conf_t *conf)
|
|
221 {
|
|
222 u_char *start, *last, *end;
|
|
223 ngx_uint_t i;
|
|
224 ngx_str_t src, dst;
|
|
225 ngx_table_elt_t **cookies;
|
|
226
|
|
227 cookies = r->headers_in.cookies.elts;
|
|
228
|
|
229 for (i = 0; i < r->headers_in.cookies.nelts; i++) {
|
|
230 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
10
|
231 "cookie: \"%V\"", &cookies[i]->value);
|
0
|
232
|
10
|
233 if (conf->name.len >= cookies[i]->value.len) {
|
|
234 continue;
|
|
235 }
|
|
236
|
|
237 start = cookies[i]->value.data;
|
0
|
238 end = cookies[i]->value.data + cookies[i]->value.len;
|
|
239
|
10
|
240 while (start < end) {
|
0
|
241
|
10
|
242 if (ngx_strncmp(start, conf->name.data, conf->name.len) != 0) {
|
|
243
|
0
|
244 while (start < end && *start++ != ';') { /* void */ }
|
10
|
245 while (start < end && *start == ' ') { start++; }
|
0
|
246
|
|
247 continue;
|
|
248 }
|
|
249
|
10
|
250 start += conf->name.len;
|
0
|
251
|
10
|
252 while (start < end && *start == ' ') { start++; }
|
|
253
|
|
254 if (start == end || *start++ != '=') {
|
|
255 /* the invalid "Cookie" header */
|
0
|
256 break;
|
|
257 }
|
|
258
|
10
|
259 while (start < end && *start == ' ') { start++; }
|
0
|
260
|
10
|
261 last = start;
|
|
262
|
|
263 while (last < end && *last++ != ';') { /* void */ }
|
0
|
264
|
|
265 if (last - start < 22) {
|
|
266 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
10
|
267 "client sent too short userid cookie \"%V\"",
|
|
268 &cookies[i]->value);
|
0
|
269 break;
|
|
270 }
|
|
271
|
|
272 /*
|
|
273 * we have to limit encoded string to 22 characters
|
|
274 * because there are already the millions cookies with a garbage
|
|
275 * instead of the correct base64 trail "=="
|
|
276 */
|
|
277
|
|
278 src.len = 22;
|
|
279 src.data = start;
|
|
280 dst.data = (u_char *) ctx->uid_got;
|
|
281
|
2
|
282 if (ngx_decode_base64(&dst, &src) == NGX_ERROR) {
|
0
|
283 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
10
|
284 "client sent invalid userid cookie \"%V\"",
|
|
285 &cookies[i]->value);
|
0
|
286 break;
|
|
287 }
|
|
288
|
|
289 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
10
|
290 "uid: %08XD%08XD%08XD%08XD",
|
0
|
291 ctx->uid_got[0], ctx->uid_got[1],
|
|
292 ctx->uid_got[2], ctx->uid_got[3]);
|
|
293
|
|
294 return NGX_OK;
|
|
295 }
|
|
296 }
|
|
297
|
|
298 return NGX_OK;
|
|
299 }
|
|
300
|
|
301
|
|
302 static ngx_int_t ngx_http_userid_set_uid(ngx_http_request_t *r,
|
|
303 ngx_http_userid_ctx_t *ctx,
|
|
304 ngx_http_userid_conf_t *conf)
|
|
305 {
|
|
306 u_char *cookie, *p;
|
|
307 size_t len;
|
|
308 socklen_t slen;
|
28
|
309 struct sockaddr_in sin;
|
0
|
310 ngx_str_t src, dst;
|
10
|
311 ngx_table_elt_t *set_cookie, *p3p;
|
0
|
312
|
|
313 /* TODO: mutex for sequencers */
|
|
314
|
|
315 if (conf->enable == NGX_HTTP_USERID_V1) {
|
|
316 if (conf->service == NGX_CONF_UNSET) {
|
|
317 ctx->uid_set[0] = 0;
|
|
318 } else {
|
|
319 ctx->uid_set[0] = htonl(conf->service);
|
|
320 }
|
|
321
|
|
322 ctx->uid_set[1] = ngx_time();
|
|
323 ctx->uid_set[2] = ngx_pid;
|
|
324 ctx->uid_set[3] = sequencer_v1;
|
|
325 sequencer_v1 += 0x100;
|
|
326
|
|
327 } else {
|
|
328 if (conf->service == NGX_CONF_UNSET) {
|
|
329 if (r->in_addr == 0) {
|
|
330 slen = sizeof(struct sockaddr_in);
|
|
331 if (getsockname(r->connection->fd,
|
28
|
332 (struct sockaddr *) &sin, &slen) == -1)
|
0
|
333 {
|
|
334 ngx_log_error(NGX_LOG_CRIT, r->connection->log,
|
28
|
335 ngx_socket_errno, "getsockname() failed");
|
0
|
336 }
|
|
337
|
28
|
338 r->in_addr = sin.sin_addr.s_addr;
|
0
|
339 }
|
|
340
|
|
341 ctx->uid_set[0] = htonl(r->in_addr);
|
|
342
|
|
343 } else {
|
|
344 ctx->uid_set[0] = htonl(conf->service);
|
|
345 }
|
|
346
|
|
347 ctx->uid_set[1] = htonl(ngx_time());
|
|
348 ctx->uid_set[2] = htonl(ngx_pid);
|
|
349 ctx->uid_set[3] = htonl(sequencer_v2);
|
|
350 sequencer_v2 += 0x100;
|
|
351 if (sequencer_v2 < 0x03030302) {
|
|
352 sequencer_v2 = 0x03030302;
|
|
353 }
|
|
354 }
|
|
355
|
10
|
356 len = conf->name.len + 1 + ngx_base64_encoded_length(16) + conf->path.len;
|
0
|
357
|
|
358 if (conf->expires) {
|
|
359 len += sizeof(expires) - 1 + 2;
|
|
360 }
|
|
361
|
28
|
362 if (conf->domain.len) {
|
10
|
363 len += conf->domain.len;
|
0
|
364 }
|
|
365
|
|
366 if (!(cookie = ngx_palloc(r->pool, len))) {
|
|
367 return NGX_ERROR;
|
|
368 }
|
|
369
|
|
370 p = ngx_cpymem(cookie, conf->name.data, conf->name.len);
|
|
371 *p++ = '=';
|
|
372
|
|
373 src.len = 16;
|
|
374 src.data = (u_char *) ctx->uid_set;
|
|
375 dst.data = p;
|
|
376
|
2
|
377 ngx_encode_base64(&dst, &src);
|
0
|
378
|
|
379 p += dst.len;
|
|
380
|
|
381 if (conf->expires == NGX_HTTP_USERID_MAX_EXPIRES) {
|
|
382 p = ngx_cpymem(p, expires, sizeof(expires) - 1);
|
|
383
|
|
384 } else if (conf->expires) {
|
|
385 p = ngx_cpymem(p, expires, sizeof("; expires=") - 1);
|
4
|
386 p = ngx_http_cookie_time(p, ngx_time() + conf->expires);
|
0
|
387 }
|
|
388
|
28
|
389 if (conf->domain.len) {
|
0
|
390 p = ngx_cpymem(p, conf->domain.data, conf->domain.len);
|
|
391 }
|
|
392
|
10
|
393 p = ngx_cpymem(p, conf->path.data, conf->path.len);
|
0
|
394
|
|
395 if (!(set_cookie = ngx_list_push(&r->headers_out.headers))) {
|
|
396 return NGX_ERROR;
|
|
397 }
|
|
398
|
|
399 set_cookie->key.len = sizeof("Set-Cookie") - 1;
|
|
400 set_cookie->key.data = (u_char *) "Set-Cookie";
|
|
401 set_cookie->value.len = p - cookie;
|
|
402 set_cookie->value.data = cookie;
|
|
403
|
10
|
404 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
405 "uid cookie: \"%V\"", &set_cookie->value);
|
|
406
|
28
|
407 if (conf->p3p.len == 0) {
|
10
|
408 return NGX_OK;
|
|
409 }
|
|
410
|
|
411 if (!(p3p = ngx_list_push(&r->headers_out.headers))) {
|
|
412 return NGX_ERROR;
|
|
413 }
|
|
414
|
|
415 p3p->key.len = sizeof("P3P") - 1;
|
|
416 p3p->key.data = (u_char *) "P3P";
|
|
417 p3p->value = conf->p3p;
|
|
418
|
0
|
419 return NGX_OK;
|
|
420 }
|
|
421
|
|
422
|
28
|
423 static size_t ngx_http_userid_log_uid_got_getlen(ngx_http_request_t *r,
|
|
424 uintptr_t data)
|
0
|
425 {
|
|
426 ngx_http_userid_ctx_t *ctx;
|
|
427 ngx_http_userid_conf_t *conf;
|
|
428
|
|
429 ctx = ngx_http_get_module_ctx(r, ngx_http_userid_filter_module);
|
|
430
|
|
431 if (ctx == NULL || ctx->uid_got[3] == 0) {
|
28
|
432 return 1;
|
|
433 }
|
|
434
|
|
435 conf = ngx_http_get_module_loc_conf(r, ngx_http_userid_filter_module);
|
|
436
|
|
437 return conf->name.len + 1 + 32;
|
|
438 }
|
|
439
|
0
|
440
|
28
|
441 static u_char *ngx_http_userid_log_uid_got(ngx_http_request_t *r, u_char *buf,
|
|
442 ngx_http_log_op_t *op)
|
|
443 {
|
|
444 ngx_http_userid_ctx_t *ctx;
|
|
445 ngx_http_userid_conf_t *conf;
|
|
446
|
|
447 ctx = ngx_http_get_module_ctx(r, ngx_http_userid_filter_module);
|
|
448
|
|
449 if (ctx == NULL || ctx->uid_got[3] == 0) {
|
0
|
450 *buf = '-';
|
|
451 return buf + 1;
|
|
452 }
|
|
453
|
|
454 conf = ngx_http_get_module_loc_conf(r, ngx_http_userid_filter_module);
|
|
455
|
|
456 buf = ngx_cpymem(buf, conf->name.data, conf->name.len);
|
|
457
|
|
458 *buf++ = '=';
|
|
459
|
10
|
460 return ngx_sprintf(buf, "%08XD%08XD%08XD%08XD",
|
|
461 ctx->uid_got[0], ctx->uid_got[1],
|
|
462 ctx->uid_got[2], ctx->uid_got[3]);
|
0
|
463 }
|
|
464
|
|
465
|
28
|
466 static size_t ngx_http_userid_log_uid_set_getlen(ngx_http_request_t *r,
|
|
467 uintptr_t data)
|
0
|
468 {
|
|
469 ngx_http_userid_ctx_t *ctx;
|
|
470 ngx_http_userid_conf_t *conf;
|
|
471
|
|
472 ctx = ngx_http_get_module_ctx(r, ngx_http_userid_filter_module);
|
|
473
|
|
474 if (ctx == NULL || ctx->uid_set[3] == 0) {
|
28
|
475 return 1;
|
|
476 }
|
|
477
|
|
478 conf = ngx_http_get_module_loc_conf(r, ngx_http_userid_filter_module);
|
|
479
|
|
480 return conf->name.len + 1 + 32;
|
|
481 }
|
|
482
|
0
|
483
|
28
|
484 static u_char *ngx_http_userid_log_uid_set(ngx_http_request_t *r, u_char *buf,
|
|
485 ngx_http_log_op_t *op)
|
|
486 {
|
|
487 ngx_http_userid_ctx_t *ctx;
|
|
488 ngx_http_userid_conf_t *conf;
|
|
489
|
|
490 ctx = ngx_http_get_module_ctx(r, ngx_http_userid_filter_module);
|
|
491
|
|
492 if (ctx == NULL || ctx->uid_set[3] == 0) {
|
0
|
493 *buf = '-';
|
|
494 return buf + 1;
|
|
495 }
|
|
496
|
|
497 conf = ngx_http_get_module_loc_conf(r, ngx_http_userid_filter_module);
|
|
498
|
|
499 buf = ngx_cpymem(buf, conf->name.data, conf->name.len);
|
|
500
|
|
501 *buf++ = '=';
|
|
502
|
10
|
503 return ngx_sprintf(buf, "%08XD%08XD%08XD%08XD",
|
|
504 ctx->uid_set[0], ctx->uid_set[1],
|
|
505 ctx->uid_set[2], ctx->uid_set[3]);
|
0
|
506 }
|
|
507
|
|
508
|
28
|
509 static ngx_int_t ngx_http_userid_add_log_formats(ngx_conf_t *cf)
|
0
|
510 {
|
28
|
511 ngx_http_log_op_name_t *op;
|
|
512
|
|
513 for (op = ngx_http_userid_log_fmt_ops; op->name.len; op++) { /* void */ }
|
|
514 op->run = NULL;
|
|
515
|
|
516 for (op = ngx_http_log_fmt_ops; op->run; op++) {
|
|
517 if (op->name.len == 0) {
|
|
518 op = (ngx_http_log_op_name_t *) op->run;
|
|
519 }
|
|
520 }
|
|
521
|
|
522 op->run = (ngx_http_log_op_run_pt) ngx_http_userid_log_fmt_ops;
|
0
|
523
|
|
524 return NGX_OK;
|
|
525 }
|
|
526
|
|
527
|
28
|
528 static ngx_int_t ngx_http_userid_init(ngx_cycle_t *cycle)
|
0
|
529 {
|
28
|
530 ngx_http_next_header_filter = ngx_http_top_header_filter;
|
|
531 ngx_http_top_header_filter = ngx_http_userid_filter;
|
0
|
532
|
|
533 return NGX_OK;
|
|
534 }
|
|
535
|
|
536
|
|
537 static void *ngx_http_userid_create_conf(ngx_conf_t *cf)
|
|
538 {
|
|
539 ngx_http_userid_conf_t *conf;
|
|
540
|
|
541 if (!(conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_userid_conf_t)))) {
|
|
542 return NGX_CONF_ERROR;
|
|
543 }
|
|
544
|
28
|
545 /*
|
|
546 * set by ngx_pcalloc():
|
|
547 *
|
|
548 * conf->name.len = 0;
|
|
549 * conf->name.date = NULL;
|
|
550 * conf->domain.len = 0;
|
|
551 * conf->domain.date = NULL;
|
|
552 * conf->path.len = 0;
|
|
553 * conf->path.date = NULL;
|
|
554 * conf->p3p.len = 0;
|
|
555 * conf->p3p.date = NULL;
|
|
556 */
|
0
|
557
|
|
558 conf->enable = NGX_CONF_UNSET;
|
|
559 conf->service = NGX_CONF_UNSET;
|
|
560 conf->expires = NGX_CONF_UNSET;
|
|
561
|
|
562 return conf;
|
|
563 }
|
|
564
|
|
565
|
|
566 static char *ngx_http_userid_merge_conf(ngx_conf_t *cf, void *parent,
|
|
567 void *child)
|
|
568 {
|
|
569 ngx_http_userid_conf_t *prev = parent;
|
|
570 ngx_http_userid_conf_t *conf = child;
|
|
571
|
|
572 ngx_conf_merge_value(conf->enable, prev->enable, NGX_HTTP_USERID_OFF);
|
|
573
|
|
574 ngx_conf_merge_str_value(conf->name, prev->name, "uid");
|
28
|
575 ngx_conf_merge_str_value(conf->domain, prev->domain, "");
|
10
|
576 ngx_conf_merge_str_value(conf->path, prev->path, "; path=/");
|
28
|
577 ngx_conf_merge_str_value(conf->p3p, prev->p3p, "");
|
0
|
578
|
|
579 ngx_conf_merge_value(conf->service, prev->service, NGX_CONF_UNSET);
|
|
580 ngx_conf_merge_sec_value(conf->expires, prev->expires, 0);
|
|
581
|
|
582 return NGX_CONF_OK;
|
|
583 }
|
|
584
|
|
585
|
10
|
586 char *ngx_http_userid_domain(ngx_conf_t *cf, void *post, void *data)
|
0
|
587 {
|
|
588 ngx_str_t *domain = data;
|
|
589
|
10
|
590 u_char *p, *new;
|
|
591
|
0
|
592 if (domain->len == 4 && ngx_strcmp(domain->data, "none") == 0) {
|
28
|
593 domain->len = 0;
|
|
594 domain->data = (u_char *) "";
|
10
|
595
|
|
596 return NGX_CONF_OK;
|
0
|
597 }
|
|
598
|
10
|
599 if (!(new = ngx_palloc(cf->pool, sizeof("; domain=") - 1 + domain->len))) {
|
|
600 return NGX_CONF_ERROR;
|
|
601 }
|
|
602
|
|
603 p = ngx_cpymem(new, "; domain=", sizeof("; domain=") - 1);
|
|
604 p = ngx_cpymem(p, domain->data, domain->len);
|
|
605
|
|
606 domain->len += sizeof("; domain=") - 1;
|
|
607 domain->data = new;
|
|
608
|
|
609 return NGX_CONF_OK;
|
|
610 }
|
|
611
|
|
612
|
|
613 char *ngx_http_userid_path(ngx_conf_t *cf, void *post, void *data)
|
|
614 {
|
|
615 ngx_str_t *path = data;
|
|
616
|
|
617 u_char *p, *new;
|
|
618
|
|
619 if (!(new = ngx_palloc(cf->pool, sizeof("; path=") - 1 + path->len))) {
|
|
620 return NGX_CONF_ERROR;
|
|
621 }
|
|
622
|
|
623 p = ngx_cpymem(new, "; path=", sizeof("; path=") - 1);
|
|
624 p = ngx_cpymem(p, path->data, path->len);
|
|
625
|
|
626 path->len += sizeof("; path=") - 1;
|
|
627 path->data = new;
|
|
628
|
0
|
629 return NGX_CONF_OK;
|
|
630 }
|
|
631
|
|
632
|
|
633 char *ngx_http_userid_expires(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
634 {
|
|
635 ngx_http_userid_conf_t *ucf = conf;
|
|
636
|
|
637 ngx_str_t *value;
|
|
638
|
|
639 if (ucf->expires != NGX_CONF_UNSET) {
|
|
640 return "is duplicate";
|
|
641 }
|
|
642
|
|
643 value = cf->args->elts;
|
|
644
|
|
645 if (ngx_strcmp(value[1].data, "max") == 0) {
|
|
646 ucf->expires = NGX_HTTP_USERID_MAX_EXPIRES;
|
|
647 return NGX_CONF_OK;
|
|
648 }
|
|
649
|
|
650 if (ngx_strcmp(value[1].data, "off") == 0) {
|
|
651 ucf->expires = 0;
|
|
652 return NGX_CONF_OK;
|
|
653 }
|
|
654
|
|
655 ucf->expires = ngx_parse_time(&value[1], 1);
|
|
656 if (ucf->expires == NGX_ERROR) {
|
|
657 return "invalid value";
|
|
658 }
|
|
659
|
|
660 if (ucf->expires == NGX_PARSE_LARGE_TIME) {
|
|
661 return "value must be less than 68 years";
|
|
662 }
|
|
663
|
|
664 return NGX_CONF_OK;
|
|
665 }
|
10
|
666
|
|
667
|
|
668 char *ngx_http_userid_p3p(ngx_conf_t *cf, void *post, void *data)
|
|
669 {
|
|
670 ngx_str_t *p3p = data;
|
|
671
|
|
672 if (p3p->len == 4 && ngx_strcmp(p3p->data, "none") == 0) {
|
28
|
673 p3p->len = 0;
|
|
674 p3p->data = (u_char *) "";
|
10
|
675 }
|
|
676
|
|
677 return NGX_CONF_OK;
|
|
678 }
|