mod_usertrack.c revision e8f95a682820a599fe41b22977010636be5c2717
/* Copyright 1999-2005 The Apache Software Foundation or its licensors, as
* applicable.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* User Tracking Module (Was mod_cookies.c)
*
* *** IMPORTANT NOTE: This module is not designed to generate
* *** cryptographically secure cookies. This means you should not
* *** use cookies generated by this module for authentication purposes
*
* This Apache module is designed to track users paths through a site.
* It uses the client-side state ("Cookie") protocol developed by Netscape.
* It is known to work on most browsers.
*
* Each time a page is requested we look to see if the browser is sending
* us a Cookie: header that we previously generated.
*
* If we don't find one then the user hasn't been to this site since
* starting their browser or their browser doesn't support cookies. So
* we generate a unique Cookie for the transaction and send it back to
* the browser (via a "Set-Cookie" header)
* Future requests from the same browser should keep the same Cookie line.
*
* By matching up all the requests with the same cookie you can
* work out exactly what path a user took through your site. To log
* the cookie use the " %{Cookie}n " directive in a custom access log;
*
* Example 1 : If you currently use the standard Log file format (CLF)
* and use the command "TransferLog somefilename", add the line
* LogFormat "%h %l %u %t \"%r\" %s %b %{Cookie}n"
* to your config file.
*
* Example 2 : If you used to use the old "CookieLog" directive, you
* can emulate it by adding the following command to your config file
* CustomLog filename "%{Cookie}n \"%r\" %t"
*
* Mark Cox, mjc@apache.org, 6 July 95
*
* This file replaces mod_cookies.c
*/
#include "apr.h"
#include "apr_lib.h"
#include "apr_strings.h"
#define APR_WANT_STRFUNC
#include "apr_want.h"
#include "httpd.h"
#include "http_config.h"
#include "http_core.h"
#include "http_request.h"
typedef struct {
int always;
int expires;
typedef enum {
typedef struct {
int enabled;
char *cookie_name;
char *cookie_domain;
char *regexp_string; /* used to compile regexp; save for debugging */
/* Make Cookie: Now we have to generate something that is going to be
* pretty unique. We can base it on the pid, time, hostip */
#define COOKIE_NAME "Apache"
static void make_cookie(request_rec *r)
{
/* 1024 == hardcoded constant */
char cookiebuf[1024];
char *new_cookie;
REMOTE_NAME, NULL);
/* XXX: hmm, this should really tie in with mod_unique_id */
apr_time_now());
/* Cookie with date; as strftime '%a, %d-%h-%y %H:%M:%S GMT' */
"%s; expires=%s, "
"%.2d-%s-%.2d %.2d:%.2d:%.2d GMT",
}
else {
}
}
else {
}
? "; version=1"
: ""),
NULL);
}
return;
}
/* dcfg->regexp is "^cookie_name=([^;]+)|;[ \t]+cookie_name=([^;]+)",
* which has three subexpressions, $0..$2 */
#define NUM_SUBS 3
apr_pool_t *p,
const char *cookie_name)
{
int danger_chars = 0;
const char *sp = cookie_name;
/* The goal is to end up with this regexp,
* ^cookie_name=([^;,]+)|[;,][ \t]+cookie_name=([^;,]+)
* with cookie_name obviously substituted either
* with the real cookie name set by the user in httpd.conf, or with the
* default COOKIE_NAME. */
/* Anyway, we need to escape the cookie_name before pasting it
* into the regex
*/
while (*sp) {
if (!apr_isalnum(*sp)) {
++danger_chars;
}
++sp;
}
if (danger_chars) {
char *cp;
sp = cookie_name;
cookie_name = cp;
while (*sp) {
if (!apr_isalnum(*sp)) {
*cp++ = '\\';
}
}
*cp = '\0';
}
"=([^;,]+)|[;,][ \t]*",
"=([^;,]+)", NULL);
}
static int spot_cookie(request_rec *r)
{
const char *cookie_header;
/* Do not run in subrequests */
return DECLINED;
}
/* Our regexp,
* ^cookie_name=([^;]+)|;[ \t]+cookie_name=([^;]+)
* only allows for $1 or $2 to be available. ($0 is always
* filled with the entire matched expression, not just
* the part in parentheses.) So just check for either one
* and assign to cookieval if present. */
}
}
/* Set the cookie in a note, for logging */
return DECLINED; /* There's already a cookie, no new one */
}
}
make_cookie(r);
return OK; /* We set our cookie */
}
{
return (void *) cls;
}
static void *make_cookie_dir(apr_pool_t *p, char *d)
{
/* In case the user does not use the CookieName directive,
* we need to compile the regexp for the default cookie name. */
return dcfg;
}
{
return NULL;
}
const char *arg)
{
char *word;
/* The simple case first - all numbers (we assume) */
return NULL;
}
/*
* The harder case - stolen from mod_expires
*
* CookieExpires "[plus] {<num> <type>}*"
*/
};
/* {<num> <type>}* */
while (word[0]) {
/* <num> */
if (apr_isdigit(word[0]))
else
return "bad expires code, numeric value expected.";
/* <type> */
if (!word[0])
return "bad expires code, missing <type>";
factor = 0;
factor = 60;
factor = 1;
else
return "bad expires code, unrecognized type";
/* next <num> */
}
return NULL;
}
const char *name)
{
return "Regular expression could not be compiled.";
}
}
return NULL;
}
/*
* Set the value for the 'Domain=' attribute.
*/
const char *name)
{
/*
* Apply the restrictions on cookie domain attributes.
*/
return "CookieDomain values may not be null";
}
if (name[0] != '.') {
return "CookieDomain values must begin with a dot";
}
return "CookieDomain values must contain at least one embedded dot";
}
return NULL;
}
/*
* Make a note of the cookie style we should use.
*/
const char *name)
{
}
}
}
else {
}
return NULL;
}
static const command_rec cookie_log_cmds[] = {
"an expiry date code"),
"domain to which this cookie applies"),
"'Netscape', 'Cookie' (RFC2109), or 'Cookie2' (RFC2965)"),
"whether or not to enable cookies"),
"name of the tracking cookie"),
{NULL}
};
static void register_hooks(apr_pool_t *p)
{
}
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 apr_table_t */
register_hooks /* register hooks */
};