mod_usertrack.c revision 6db54801798f6021bb464de7b3fdcc9dc3150f7b
d29201dd5328b88140ce050100693c501852657dChristian Maeder/* ====================================================================
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * Copyright (c) 1995-1999 The Apache Group. All rights reserved.
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder *
7165a916d2fa1bf87c4741ec63b253413eebbf69Karl Luc * Redistribution and use in source and binary forms, with or without
6d81916b9004f8d9b6032113c5987ab07da47015Karl Luc * modification, are permitted provided that the following conditions
98890889ffb2e8f6f722b00e265a211f13b5a861Corneliu-Claudiu Prodescu * are met:
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder *
7165a916d2fa1bf87c4741ec63b253413eebbf69Karl Luc * 1. Redistributions of source code must retain the above copyright
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * notice, this list of conditions and the following disclaimer.
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder *
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * 2. Redistributions in binary form must reproduce the above copyright
7165a916d2fa1bf87c4741ec63b253413eebbf69Karl Luc * notice, this list of conditions and the following disclaimer in
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * the documentation and/or other materials provided with the
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * distribution.
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder *
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * 3. All advertising materials mentioning features or use of this
7165a916d2fa1bf87c4741ec63b253413eebbf69Karl Luc * software must display the following acknowledgment:
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * "This product includes software developed by the Apache Group
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * for use in the Apache HTTP server project (http://www.apache.org/)."
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc *
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * 4. The names "Apache Server" and "Apache Group" must not be used to
7165a916d2fa1bf87c4741ec63b253413eebbf69Karl Luc * endorse or promote products derived from this software without
7165a916d2fa1bf87c4741ec63b253413eebbf69Karl Luc * prior written permission. For written permission, please contact
d6ce032cac688f3698be7133d27f53d3967e6749Christian Maeder * apache@apache.org.
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc *
de03ed90ff6efbbc4751301bcf6b50d9790c1afbKarl Luc * 5. Products derived from this software may not be called "Apache"
d75d2d11170f1339ebe37d9d9c06aff148637b13Christian Maeder * nor may "Apache" appear in their names without prior written
66977d201b3ff7ee9c1f992c0f3f701b69eac2c9Karl Luc * permission of the Apache Group.
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder *
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * 6. Redistributions of any form whatsoever must retain the following
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * acknowledgment:
0ac66bed6456d7900a657269e36107ca7bec4756Iulia Ignatov * "This product includes software developed by the Apache Group
0ac66bed6456d7900a657269e36107ca7bec4756Iulia Ignatov * for use in the Apache HTTP server project (http://www.apache.org/)."
0ac66bed6456d7900a657269e36107ca7bec4756Iulia Ignatov *
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
a35bacbc16daf5c10f9accfdfadc4971e9d6f648Iulia Ignatov * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3831cf8a3b0ea144a80d13fe0314cc2752e32107Christian Maeder * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
7165a916d2fa1bf87c4741ec63b253413eebbf69Karl Luc * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * OF THE POSSIBILITY OF SUCH DAMAGE.
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * ====================================================================
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc *
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * This software consists of voluntary contributions made by many
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * individuals on behalf of the Apache Group and was originally based
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * on public domain software written at the National Center for
7165a916d2fa1bf87c4741ec63b253413eebbf69Karl Luc * Supercomputing Applications, University of Illinois, Urbana-Champaign.
2c10bceb28a74fc291959697e023f22a66753655Eugen Kuksa * For more information on the Apache Group and the Apache HTTP server
d6ce032cac688f3698be7133d27f53d3967e6749Christian Maeder * project, please see <http://www.apache.org/>.
7165a916d2fa1bf87c4741ec63b253413eebbf69Karl Luc *
de03ed90ff6efbbc4751301bcf6b50d9790c1afbKarl Luc */
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc
551f1476dea3f969775527cb15fd512e86279307Karl Luc/* User Tracking Module (Was mod_cookies.c)
9e5811a3323b00d6ff8d3fe91a185de1ab31150bChristian Maeder *
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * This Apache module is designed to track users paths through a site.
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * It uses the client-side state ("Cookie") protocol developed by Netscape.
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * It is known to work on Netscape browsers, Microsoft Internet
7165a916d2fa1bf87c4741ec63b253413eebbf69Karl Luc * Explorer and others currently being developed.
7165a916d2fa1bf87c4741ec63b253413eebbf69Karl Luc *
c51d1f5ff88cce030fe543e271ca6b85625b70d8Karl Luc * Each time a page is requested we look to see if the browser is sending
037be4e5b0e867dd148db2ea89640d8edf009053Eugen Kuksa * us a Cookie: header that we previously generated.
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc *
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * If we don't find one then the user hasn't been to this site since
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * starting their browser or their browser doesn't support cookies. So
037be4e5b0e867dd148db2ea89640d8edf009053Eugen Kuksa * we generate a unique Cookie for the transaction and send it back to
037be4e5b0e867dd148db2ea89640d8edf009053Eugen Kuksa * the browser (via a "Set-Cookie" header)
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * Future requests from the same browser should keep the same Cookie line.
3831cf8a3b0ea144a80d13fe0314cc2752e32107Christian Maeder *
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa * By matching up all the requests with the same cookie you can
c51d1f5ff88cce030fe543e271ca6b85625b70d8Karl Luc * work out exactly what path a user took through your site. To log
2c10bceb28a74fc291959697e023f22a66753655Eugen Kuksa * the cookie use the " %{Cookie}n " directive in a custom access log;
037be4e5b0e867dd148db2ea89640d8edf009053Eugen Kuksa *
2c10bceb28a74fc291959697e023f22a66753655Eugen Kuksa * Example 1 : If you currently use the standard Log file format (CLF)
7165a916d2fa1bf87c4741ec63b253413eebbf69Karl Luc * and use the command "TransferLog somefilename", add the line
d6ce032cac688f3698be7133d27f53d3967e6749Christian Maeder * LogFormat "%h %l %u %t \"%r\" %s %b %{Cookie}n"
de03ed90ff6efbbc4751301bcf6b50d9790c1afbKarl Luc * to your config file.
de03ed90ff6efbbc4751301bcf6b50d9790c1afbKarl Luc *
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * Example 2 : If you used to use the old "CookieLog" directive, you
551f1476dea3f969775527cb15fd512e86279307Karl Luc * can emulate it by adding the following command to your config file
5e6915f767bd46767306abbfbb39ebcbc10a2a33Christian Maeder * CustomLog filename "%{Cookie}n \"%r\" %t"
7b1111ca3b126f71cce47e60ce4b56e6f92422e9Karl Luc *
7b1111ca3b126f71cce47e60ce4b56e6f92422e9Karl Luc * Notes:
0ac66bed6456d7900a657269e36107ca7bec4756Iulia Ignatov * 1. This code now logs the initial transaction (the one that created
0ac66bed6456d7900a657269e36107ca7bec4756Iulia Ignatov * the cookie to start with).
0ac66bed6456d7900a657269e36107ca7bec4756Iulia Ignatov * 2. This module has been designed to not interfere with other Cookies
0ac66bed6456d7900a657269e36107ca7bec4756Iulia Ignatov * your site may be using; just avoid sending out cookies with
0ac66bed6456d7900a657269e36107ca7bec4756Iulia Ignatov * the name "Apache=" or things will get confused.
0ac66bed6456d7900a657269e36107ca7bec4756Iulia Ignatov * 3. If you want you can modify the Set-Cookie line so that the Cookie
51846950b4b1f31342008cf17f667859a5f21949Christian Maeder * never expires. You would then get the same Cookie each time the
3831cf8a3b0ea144a80d13fe0314cc2752e32107Christian Maeder * user revisits your site.
c51d1f5ff88cce030fe543e271ca6b85625b70d8Karl Luc *
2c10bceb28a74fc291959697e023f22a66753655Eugen Kuksa * Mark Cox, mark@ukweb.com, 6 July 95
037be4e5b0e867dd148db2ea89640d8edf009053Eugen Kuksa *
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * This file replaces mod_cookies.c
de03ed90ff6efbbc4751301bcf6b50d9790c1afbKarl Luc */
7165a916d2fa1bf87c4741ec63b253413eebbf69Karl Luc
de03ed90ff6efbbc4751301bcf6b50d9790c1afbKarl Luc#include "httpd.h"
de03ed90ff6efbbc4751301bcf6b50d9790c1afbKarl Luc#include "http_config.h"
7165a916d2fa1bf87c4741ec63b253413eebbf69Karl Luc#include "http_core.h"
9e5811a3323b00d6ff8d3fe91a185de1ab31150bChristian Maeder#include "http_request.h"
9e5811a3323b00d6ff8d3fe91a185de1ab31150bChristian Maeder
9e5811a3323b00d6ff8d3fe91a185de1ab31150bChristian Maedermodule MODULE_VAR_EXPORT usertrack_module;
9e5811a3323b00d6ff8d3fe91a185de1ab31150bChristian Maeder
9e5811a3323b00d6ff8d3fe91a185de1ab31150bChristian Maedertypedef struct {
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc int always;
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc int expires;
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc} cookie_log_state;
9e5811a3323b00d6ff8d3fe91a185de1ab31150bChristian Maeder
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luctypedef struct {
9e5811a3323b00d6ff8d3fe91a185de1ab31150bChristian Maeder int enabled;
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc char *cookie_name;
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc} cookie_dir_rec;
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc/* Make Cookie: Now we have to generate something that is going to be
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc * pretty unique. We can base it on the pid, time, hostip */
75067b1beba1380cde707c30e7fc050d86f6927fKarl Luc
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa#define COOKIE_NAME "Apache"
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksastatic void make_cookie(request_rec *r)
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa{
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa cookie_log_state *cls = ap_get_module_config(r->server->module_config,
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa &usertrack_module);
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa /* 1024 == hardcoded constant */
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa char cookiebuf[1024];
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa char *new_cookie;
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa const char *rname = ap_get_remote_host(r->connection, r->per_dir_config,
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa REMOTE_NAME);
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa cookie_dir_rec *dcfg;
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa dcfg = ap_get_module_config(r->per_dir_config, &usertrack_module);
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa /* XXX: hmm, this should really tie in with mod_unique_id */
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa ap_snprintf(cookiebuf, sizeof(cookiebuf), "%s.%qd", rname, ap_now());
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa if (cls->expires) {
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa ap_exploded_time_t tms;
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa ap_explode_gmt(&tms, r->request_time + cls->expires * AP_USEC_PER_SEC);
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa /* Cookie with date; as strftime '%a, %d-%h-%y %H:%M:%S GMT' */
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa new_cookie = ap_psprintf(r->pool,
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa "%s=%s; path=/; expires=%s, %.2d-%s-%.2d %.2d:%.2d:%.2d GMT",
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa dcfg->cookie_name, cookiebuf, ap_day_snames[tms.tm_wday],
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa tms.tm_mday, ap_month_snames[tms.tm_mon],
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa tms.tm_year % 100,
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa tms.tm_hour, tms.tm_min, tms.tm_sec);
037be4e5b0e867dd148db2ea89640d8edf009053Eugen Kuksa }
037be4e5b0e867dd148db2ea89640d8edf009053Eugen Kuksa else {
037be4e5b0e867dd148db2ea89640d8edf009053Eugen Kuksa new_cookie = ap_psprintf(r->pool, "%s=%s; path=/",
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa dcfg->cookie_name, cookiebuf);
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa }
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa ap_table_setn(r->headers_out, "Set-Cookie", new_cookie);
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa ap_table_setn(r->notes, "cookie", ap_pstrdup(r->pool, cookiebuf)); /* log first time */
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa return;
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa}
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksastatic int spot_cookie(request_rec *r)
037be4e5b0e867dd148db2ea89640d8edf009053Eugen Kuksa{
037be4e5b0e867dd148db2ea89640d8edf009053Eugen Kuksa cookie_dir_rec *dcfg = ap_get_module_config(r->per_dir_config,
037be4e5b0e867dd148db2ea89640d8edf009053Eugen Kuksa &usertrack_module);
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa const char *cookie;
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa char *value;
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa if (!dcfg->enabled) {
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa return DECLINED;
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa }
99a1bfbb82c875580410b4745617fea2297fdc2bEugen Kuksa
5e6915f767bd46767306abbfbb39ebcbc10a2a33Christian Maeder if ((cookie = ap_table_get(r->headers_in, "Cookie")))
if ((value = strstr(cookie, dcfg->cookie_name))) {
char *cookiebuf, *cookieend;
value += strlen(dcfg->cookie_name) + 1; /* Skip over the '=' */
cookiebuf = ap_pstrdup(r->pool, value);
cookieend = strchr(cookiebuf, ';');
if (cookieend)
*cookieend = '\0'; /* Ignore anything after a ; */
/* Set the cookie in a note, for logging */
ap_table_setn(r->notes, "cookie", cookiebuf);
return DECLINED; /* There's already a cookie, no new one */
}
make_cookie(r);
return OK; /* We set our cookie */
}
static void *make_cookie_log_state(ap_context_t *p, server_rec *s)
{
cookie_log_state *cls =
(cookie_log_state *) ap_palloc(p, sizeof(cookie_log_state));
cls->expires = 0;
return (void *) cls;
}
static void *make_cookie_dir(ap_context_t *p, char *d)
{
cookie_dir_rec *dcfg;
dcfg = (cookie_dir_rec *) ap_pcalloc(p, sizeof(cookie_dir_rec));
dcfg->cookie_name = COOKIE_NAME;
dcfg->enabled = 0;
return dcfg;
}
static const char *set_cookie_enable(cmd_parms *cmd, void *mconfig, int arg)
{
cookie_dir_rec *dcfg = mconfig;
dcfg->enabled = arg;
return NULL;
}
static const char *set_cookie_exp(cmd_parms *parms, void *dummy, const char *arg)
{
cookie_log_state *cls = ap_get_module_config(parms->server->module_config,
&usertrack_module);
time_t factor, modifier = 0;
time_t num = 0;
char *word;
/* The simple case first - all numbers (we assume) */
if (ap_isdigit(arg[0]) && ap_isdigit(arg[strlen(arg) - 1])) {
cls->expires = atol(arg);
return NULL;
}
/*
* The harder case - stolen from mod_expires
*
* CookieExpires "[plus] {<num> <type>}*"
*/
word = ap_getword_conf(parms->pool, &arg);
if (!strncasecmp(word, "plus", 1)) {
word = ap_getword_conf(parms->pool, &arg);
};
/* {<num> <type>}* */
while (word[0]) {
/* <num> */
if (ap_isdigit(word[0]))
num = atoi(word);
else
return "bad expires code, numeric value expected.";
/* <type> */
word = ap_getword_conf(parms->pool, &arg);
if (!word[0])
return "bad expires code, missing <type>";
factor = 0;
if (!strncasecmp(word, "years", 1))
factor = 60 * 60 * 24 * 365;
else if (!strncasecmp(word, "months", 2))
factor = 60 * 60 * 24 * 30;
else if (!strncasecmp(word, "weeks", 1))
factor = 60 * 60 * 24 * 7;
else if (!strncasecmp(word, "days", 1))
factor = 60 * 60 * 24;
else if (!strncasecmp(word, "hours", 1))
factor = 60 * 60;
else if (!strncasecmp(word, "minutes", 2))
factor = 60;
else if (!strncasecmp(word, "seconds", 1))
factor = 1;
else
return "bad expires code, unrecognized type";
modifier = modifier + factor * num;
/* next <num> */
word = ap_getword_conf(parms->pool, &arg);
}
cls->expires = modifier;
return NULL;
}
static const char *set_cookie_name(cmd_parms *cmd, void *mconfig, char *name)
{
cookie_dir_rec *dcfg = (cookie_dir_rec *) mconfig;
dcfg->cookie_name = ap_pstrdup(cmd->pool, name);
return NULL;
}
static const command_rec cookie_log_cmds[] = {
{"CookieExpires", set_cookie_exp, NULL, RSRC_CONF, TAKE1,
"an expiry date code"},
{"CookieTracking", set_cookie_enable, NULL, OR_FILEINFO, FLAG,
"whether or not to enable cookies"},
{"CookieName", set_cookie_name, NULL, OR_FILEINFO, TAKE1,
"name of the tracking cookie"},
{NULL}
};
static void register_hooks(void)
{
ap_hook_fixups(spot_cookie,NULL,NULL,HOOK_MIDDLE);
}
module MODULE_VAR_EXPORT usertrack_module = {
STANDARD20_MODULE_STUFF,
make_cookie_dir, /* dir config creater */
NULL, /* dir merger --- default is to override */
make_cookie_log_state, /* server config */
NULL, /* merge server configs */
cookie_log_cmds, /* command ap_table_t */
NULL, /* handlers */
register_hooks /* register hooks */
};