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