[mod_cgi] fix pipe_cloexec() when no O_CLOEXEC
[lighttpd.git] / src / mod_usertrack.c
blob875e0393818b447d16214d9705e2d0f455167262
1 #include "first.h"
3 #include "base.h"
4 #include "log.h"
5 #include "buffer.h"
6 #include "rand.h"
8 #include "plugin.h"
10 #include <ctype.h>
11 #include <stdlib.h>
12 #include <string.h>
14 #include "md5.h"
16 /* plugin config for all request/connections */
18 typedef struct {
19 buffer *cookie_name;
20 buffer *cookie_domain;
21 unsigned int cookie_max_age;
22 } plugin_config;
24 typedef struct {
25 PLUGIN_DATA;
27 plugin_config **config_storage;
29 plugin_config conf;
30 } plugin_data;
32 /* init the plugin data */
33 INIT_FUNC(mod_usertrack_init) {
34 plugin_data *p;
36 p = calloc(1, sizeof(*p));
38 return p;
41 /* detroy the plugin data */
42 FREE_FUNC(mod_usertrack_free) {
43 plugin_data *p = p_d;
45 UNUSED(srv);
47 if (!p) return HANDLER_GO_ON;
49 if (p->config_storage) {
50 size_t i;
51 for (i = 0; i < srv->config_context->used; i++) {
52 plugin_config *s = p->config_storage[i];
54 if (NULL == s) continue;
56 buffer_free(s->cookie_name);
57 buffer_free(s->cookie_domain);
59 free(s);
61 free(p->config_storage);
64 free(p);
66 return HANDLER_GO_ON;
69 /* handle plugin config and check values */
71 SETDEFAULTS_FUNC(mod_usertrack_set_defaults) {
72 plugin_data *p = p_d;
73 size_t i = 0;
75 config_values_t cv[] = {
76 { "usertrack.cookie-name", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 0 */
77 { "usertrack.cookie-max-age", NULL, T_CONFIG_INT, T_CONFIG_SCOPE_CONNECTION }, /* 1 */
78 { "usertrack.cookie-domain", NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION }, /* 2 */
80 { NULL, NULL, T_CONFIG_UNSET, T_CONFIG_SCOPE_UNSET }
83 if (!p) return HANDLER_ERROR;
85 p->config_storage = calloc(1, srv->config_context->used * sizeof(plugin_config *));
87 for (i = 0; i < srv->config_context->used; i++) {
88 data_config const* config = (data_config const*)srv->config_context->data[i];
89 plugin_config *s;
91 s = calloc(1, sizeof(plugin_config));
92 s->cookie_name = buffer_init();
93 s->cookie_domain = buffer_init();
94 s->cookie_max_age = 0;
96 cv[0].destination = s->cookie_name;
97 cv[1].destination = &(s->cookie_max_age);
98 cv[2].destination = s->cookie_domain;
100 p->config_storage[i] = s;
102 if (0 != config_insert_values_global(srv, config->value, cv, i == 0 ? T_CONFIG_SCOPE_SERVER : T_CONFIG_SCOPE_CONNECTION)) {
103 return HANDLER_ERROR;
106 if (buffer_string_is_empty(s->cookie_name)) {
107 buffer_copy_string_len(s->cookie_name, CONST_STR_LEN("TRACKID"));
108 } else {
109 size_t j, len = buffer_string_length(s->cookie_name);
110 for (j = 0; j < len; j++) {
111 char c = s->cookie_name->ptr[j] | 32;
112 if (c < 'a' || c > 'z') {
113 log_error_write(srv, __FILE__, __LINE__, "sb",
114 "invalid character in usertrack.cookie-name:",
115 s->cookie_name);
117 return HANDLER_ERROR;
122 if (!buffer_string_is_empty(s->cookie_domain)) {
123 size_t j, len = buffer_string_length(s->cookie_domain);
124 for (j = 0; j < len; j++) {
125 char c = s->cookie_domain->ptr[j];
126 if (c <= 32 || c >= 127 || c == '"' || c == '\\') {
127 log_error_write(srv, __FILE__, __LINE__, "sb",
128 "invalid character in usertrack.cookie-domain:",
129 s->cookie_domain);
131 return HANDLER_ERROR;
137 return HANDLER_GO_ON;
140 #define PATCH(x) \
141 p->conf.x = s->x;
142 static int mod_usertrack_patch_connection(server *srv, connection *con, plugin_data *p) {
143 size_t i, j;
144 plugin_config *s = p->config_storage[0];
146 PATCH(cookie_name);
147 PATCH(cookie_domain);
148 PATCH(cookie_max_age);
150 /* skip the first, the global context */
151 for (i = 1; i < srv->config_context->used; i++) {
152 data_config *dc = (data_config *)srv->config_context->data[i];
153 s = p->config_storage[i];
155 /* condition didn't match */
156 if (!config_check_cond(srv, con, dc)) continue;
158 /* merge config */
159 for (j = 0; j < dc->value->used; j++) {
160 data_unset *du = dc->value->data[j];
162 if (buffer_is_equal_string(du->key, CONST_STR_LEN("usertrack.cookie-name"))) {
163 PATCH(cookie_name);
164 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("usertrack.cookie-max-age"))) {
165 PATCH(cookie_max_age);
166 } else if (buffer_is_equal_string(du->key, CONST_STR_LEN("usertrack.cookie-domain"))) {
167 PATCH(cookie_domain);
172 return 0;
174 #undef PATCH
176 URIHANDLER_FUNC(mod_usertrack_uri_handler) {
177 plugin_data *p = p_d;
178 data_string *ds;
179 unsigned char h[16];
180 li_MD5_CTX Md5Ctx;
181 char hh[LI_ITOSTRING_LENGTH];
183 if (buffer_is_empty(con->uri.path)) return HANDLER_GO_ON;
185 mod_usertrack_patch_connection(srv, con, p);
187 if (NULL != (ds = (data_string *)array_get_element(con->request.headers, "Cookie"))) {
188 char *g;
189 /* we have a cookie, does it contain a valid name ? */
191 /* parse the cookie
193 * check for cookiename + (WS | '=')
197 if (NULL != (g = strstr(ds->value->ptr, p->conf.cookie_name->ptr))) {
198 char *nc;
200 /* skip WS */
201 for (nc = g + buffer_string_length(p->conf.cookie_name); *nc == ' ' || *nc == '\t'; nc++);
203 if (*nc == '=') {
204 /* ok, found the key of our own cookie */
206 if (strlen(nc) > 32) {
207 /* i'm lazy */
208 return HANDLER_GO_ON;
214 /* set a cookie */
215 if (NULL == (ds = (data_string *)array_get_unused_element(con->response.headers, TYPE_STRING))) {
216 ds = data_response_init();
218 buffer_copy_string_len(ds->key, CONST_STR_LEN("Set-Cookie"));
219 buffer_copy_buffer(ds->value, p->conf.cookie_name);
220 buffer_append_string_len(ds->value, CONST_STR_LEN("="));
223 /* taken from mod_auth.c */
225 /* generate shared-secret */
226 li_MD5_Init(&Md5Ctx);
227 li_MD5_Update(&Md5Ctx, CONST_BUF_LEN(con->uri.path));
228 li_MD5_Update(&Md5Ctx, CONST_STR_LEN("+"));
230 /* we assume sizeof(time_t) == 4 here, but if not it ain't a problem at all */
231 li_itostrn(hh, sizeof(hh), srv->cur_ts);
232 li_MD5_Update(&Md5Ctx, (unsigned char *)hh, strlen(hh));
233 li_MD5_Update(&Md5Ctx, (unsigned char *)srv->entropy, sizeof(srv->entropy));
234 li_itostrn(hh, sizeof(hh), li_rand());
235 li_MD5_Update(&Md5Ctx, (unsigned char *)hh, strlen(hh));
237 li_MD5_Final(h, &Md5Ctx);
239 buffer_append_string_encoded(ds->value, (char *)h, 16, ENCODING_HEX);
240 buffer_append_string_len(ds->value, CONST_STR_LEN("; Path=/"));
241 buffer_append_string_len(ds->value, CONST_STR_LEN("; Version=1"));
243 if (!buffer_string_is_empty(p->conf.cookie_domain)) {
244 buffer_append_string_len(ds->value, CONST_STR_LEN("; Domain="));
245 buffer_append_string_encoded(ds->value, CONST_BUF_LEN(p->conf.cookie_domain), ENCODING_REL_URI);
248 if (p->conf.cookie_max_age) {
249 buffer_append_string_len(ds->value, CONST_STR_LEN("; max-age="));
250 buffer_append_int(ds->value, p->conf.cookie_max_age);
253 array_insert_unique(con->response.headers, (data_unset *)ds);
255 return HANDLER_GO_ON;
258 /* this function is called at dlopen() time and inits the callbacks */
260 int mod_usertrack_plugin_init(plugin *p);
261 int mod_usertrack_plugin_init(plugin *p) {
262 p->version = LIGHTTPD_VERSION_ID;
263 p->name = buffer_init_string("usertrack");
265 p->init = mod_usertrack_init;
266 p->handle_uri_clean = mod_usertrack_uri_handler;
267 p->set_defaults = mod_usertrack_set_defaults;
268 p->cleanup = mod_usertrack_free;
270 p->data = NULL;
272 return 0;