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