mod_usertrack.c revision 0329db7881b698897213e816552b0ab31f8d4d56
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye/* ====================================================================
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * The Apache Software License, Version 1.1
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye *
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * reserved.
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye *
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * Redistribution and use in source and binary forms, with or without
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * modification, are permitted provided that the following conditions
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * are met:
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye *
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * 1. Redistributions of source code must retain the above copyright
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * notice, this list of conditions and the following disclaimer.
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye *
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * 2. Redistributions in binary form must reproduce the above copyright
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * notice, this list of conditions and the following disclaimer in
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * the documentation and/or other materials provided with the
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * distribution.
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye *
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * 3. The end-user documentation included with the redistribution,
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * if any, must include the following acknowledgment:
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * "This product includes software developed by the
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * Apache Software Foundation (http://www.apache.org/)."
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * Alternately, this acknowledgment may appear in the software itself,
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * if and wherever such third-party acknowledgments normally appear.
cf1f7b5e81583dfca30972cfef322266a6928e7fKnut Anders Hatlen *
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * 4. The names "Apache" and "Apache Software Foundation" must
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * not be used to endorse or promote products derived from this
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * software without prior written permission. For written
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * permission, please contact apache@apache.org.
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye *
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * 5. Products derived from this software may not be called "Apache",
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * nor may "Apache" appear in their name, without prior written
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * permission of the Apache Software Foundation.
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye *
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * SUCH DAMAGE.
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * ====================================================================
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye *
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * This software consists of voluntary contributions made by many
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * individuals on behalf of the Apache Software Foundation. For more
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * information on the Apache Software Foundation, please see
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * <http://www.apache.org/>.
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye *
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * Portions of this software are based upon public domain software
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * originally written at the National Center for Supercomputing Applications,
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * University of Illinois, Urbana-Champaign.
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye */
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye/* User Tracking Module (Was mod_cookies.c)
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye *
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * *** IMPORTANT NOTE: This module is not designed to generate
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * *** cryptographically secure cookies. This means you should not
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * *** use cookies generated by this module for authentication purposes
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye *
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * This Apache module is designed to track users paths through a site.
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * It uses the client-side state ("Cookie") protocol developed by Netscape.
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * It is known to work on most browsers.
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye *
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * Each time a page is requested we look to see if the browser is sending
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * us a Cookie: header that we previously generated.
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye *
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * If we don't find one then the user hasn't been to this site since
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * starting their browser or their browser doesn't support cookies. So
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * we generate a unique Cookie for the transaction and send it back to
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * the browser (via a "Set-Cookie" header)
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * Future requests from the same browser should keep the same Cookie line.
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye *
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * By matching up all the requests with the same cookie you can
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * work out exactly what path a user took through your site. To log
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * the cookie use the " %{Cookie}n " directive in a custom access log;
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye *
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * Example 1 : If you currently use the standard Log file format (CLF)
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * and use the command "TransferLog somefilename", add the line
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * LogFormat "%h %l %u %t \"%r\" %s %b %{Cookie}n"
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * to your config file.
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye *
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * Example 2 : If you used to use the old "CookieLog" directive, you
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * can emulate it by adding the following command to your config file
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye * CustomLog filename "%{Cookie}n \"%r\" %t"
0a0811923cbbd2976425db6f4c78eed811c2825bKnut Anders Hatlen *
26eda24db2dc16f2cc45055efb39762fafd606deKnut Anders Hatlen * Mark Cox, mjc@apache.org, 6 July 95
a07b2874263e3c5f0cd2e83441719415d53059c2Knut Anders Hatlen *
c7eb123c8b2081a261deff3c401fbf92ddba1b58Jorgen Austvik * This file replaces mod_cookies.c
c7eb123c8b2081a261deff3c401fbf92ddba1b58Jorgen Austvik */
49f592091468eac515dde6139fbc8efa26056b0aJorgen Austvik
945f4c3c36a15447913781dfb1894b34f2941c57Jorgen Austvik#include "apr.h"
5a0c5ad4116f5a4dd0dd5a0a4e6d02973cd5eef9Lubos Kosco#include "apr_lib.h"
780cc7d1b57609ff15fb283201e93cb501ebe9e6Jorgen Austvik#include "apr_strings.h"
780cc7d1b57609ff15fb283201e93cb501ebe9e6Jorgen Austvik
780cc7d1b57609ff15fb283201e93cb501ebe9e6Jorgen Austvik#define APR_WANT_STRFUNC
d3d2404f9a49bf70b124053feabe666f85ef5361Knut Anders Hatlen#include "apr_want.h"
d3d2404f9a49bf70b124053feabe666f85ef5361Knut Anders Hatlen
780cc7d1b57609ff15fb283201e93cb501ebe9e6Jorgen Austvik#include "httpd.h"
780cc7d1b57609ff15fb283201e93cb501ebe9e6Jorgen Austvik#include "http_config.h"
7b9f9a1761f76744fc3772181877d5e301f122adKnut Anders Hatlen#include "http_core.h"
5a0c5ad4116f5a4dd0dd5a0a4e6d02973cd5eef9Lubos Kosco#include "http_request.h"
0466de7c67573e1ce5e0733325c1e5383270f5d5Knut Anders Hatlen
0466de7c67573e1ce5e0733325c1e5383270f5d5Knut Anders Hatlen
0466de7c67573e1ce5e0733325c1e5383270f5d5Knut Anders Hatlenmodule AP_MODULE_DECLARE_DATA usertrack_module;
0466de7c67573e1ce5e0733325c1e5383270f5d5Knut Anders Hatlen
5a0c5ad4116f5a4dd0dd5a0a4e6d02973cd5eef9Lubos Koscotypedef struct {
5a0c5ad4116f5a4dd0dd5a0a4e6d02973cd5eef9Lubos Kosco int always;
5a0c5ad4116f5a4dd0dd5a0a4e6d02973cd5eef9Lubos Kosco int expires;
1ed6b730409d4740e941142599767d5eac7e7d92Lubos Kosco} cookie_log_state;
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austviktypedef enum {
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik CT_UNSET,
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik CT_NETSCAPE,
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik CT_COOKIE,
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik CT_COOKIE2
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik} cookie_type_e;
c7eb123c8b2081a261deff3c401fbf92ddba1b58Jorgen Austvik
5a0c5ad4116f5a4dd0dd5a0a4e6d02973cd5eef9Lubos Koscotypedef struct {
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik int enabled;
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik cookie_type_e style;
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik char *cookie_name;
49f592091468eac515dde6139fbc8efa26056b0aJorgen Austvik char *cookie_domain;
49f592091468eac515dde6139fbc8efa26056b0aJorgen Austvik} cookie_dir_rec;
5a0c5ad4116f5a4dd0dd5a0a4e6d02973cd5eef9Lubos Kosco
49f592091468eac515dde6139fbc8efa26056b0aJorgen Austvik/* Make Cookie: Now we have to generate something that is going to be
49f592091468eac515dde6139fbc8efa26056b0aJorgen Austvik * pretty unique. We can base it on the pid, time, hostip */
945f4c3c36a15447913781dfb1894b34f2941c57Jorgen Austvik
945f4c3c36a15447913781dfb1894b34f2941c57Jorgen Austvik#define COOKIE_NAME "Apache"
5a0c5ad4116f5a4dd0dd5a0a4e6d02973cd5eef9Lubos Kosco
945f4c3c36a15447913781dfb1894b34f2941c57Jorgen Austvikstatic void make_cookie(request_rec *r)
945f4c3c36a15447913781dfb1894b34f2941c57Jorgen Austvik{
945f4c3c36a15447913781dfb1894b34f2941c57Jorgen Austvik cookie_log_state *cls = ap_get_module_config(r->server->module_config,
1ed6b730409d4740e941142599767d5eac7e7d92Lubos Kosco &usertrack_module);
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik /* 1024 == hardcoded constant */
1ed6b730409d4740e941142599767d5eac7e7d92Lubos Kosco char cookiebuf[1024];
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik char *new_cookie;
1ed6b730409d4740e941142599767d5eac7e7d92Lubos Kosco const char *rname = ap_get_remote_host(r->connection, r->per_dir_config,
1ed6b730409d4740e941142599767d5eac7e7d92Lubos Kosco REMOTE_NAME, NULL);
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye cookie_dir_rec *dcfg;
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye dcfg = ap_get_module_config(r->per_dir_config, &usertrack_module);
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye /* XXX: hmm, this should really tie in with mod_unique_id */
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye apr_snprintf(cookiebuf, sizeof(cookiebuf), "%s.%" APR_TIME_T_FMT, rname,
3ef4850ec38b34ea6eda02c22a70ac98500584c1Trond Norbye apr_time_now());
3ef4850ec38b34ea6eda02c22a70ac98500584c1Trond Norbye
900f57f08857f08b0f9d0edcace811aed617ad7bScott Halstead if (cls->expires) {
900f57f08857f08b0f9d0edcace811aed617ad7bScott Halstead
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye /* Cookie with date; as strftime '%a, %d-%h-%y %H:%M:%S GMT' */
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye new_cookie = apr_psprintf(r->pool, "%s=%s; path=/",
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye dcfg->cookie_name, cookiebuf);
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye
225d5411e0f1f0e690e3553aad7a97c648d566a1HemangLavana if ((dcfg->style == CT_UNSET) || (dcfg->style == CT_NETSCAPE)) {
225d5411e0f1f0e690e3553aad7a97c648d566a1HemangLavana apr_time_exp_t tms;
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye apr_time_exp_gmt(&tms, r->request_time
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye + apr_time_from_sec(cls->expires));
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye new_cookie = apr_psprintf(r->pool,
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye "%s; expires=%s, "
75640e2b0da81c240758d747e76d30acd1ed194dKnut Anders Hatlen "%.2d-%s-%.2d %.2d:%.2d:%.2d GMT",
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye new_cookie, apr_day_snames[tms.tm_wday],
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye tms.tm_mday,
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye apr_month_snames[tms.tm_mon],
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye tms.tm_year % 100,
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye tms.tm_hour, tms.tm_min, tms.tm_sec);
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye }
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye else {
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye new_cookie = apr_psprintf(r->pool, "%s; max-age=%d",
7b046969a1b2565787df8ae3a8126359e8cd6fafTrond Norbye new_cookie, cls->expires);
7b046969a1b2565787df8ae3a8126359e8cd6fafTrond Norbye }
7b046969a1b2565787df8ae3a8126359e8cd6fafTrond Norbye }
7b046969a1b2565787df8ae3a8126359e8cd6fafTrond Norbye else {
7b046969a1b2565787df8ae3a8126359e8cd6fafTrond Norbye new_cookie = apr_psprintf(r->pool, "%s=%s; path=/",
7b046969a1b2565787df8ae3a8126359e8cd6fafTrond Norbye dcfg->cookie_name, cookiebuf);
7b046969a1b2565787df8ae3a8126359e8cd6fafTrond Norbye }
7b046969a1b2565787df8ae3a8126359e8cd6fafTrond Norbye if (dcfg->cookie_domain != NULL) {
7b046969a1b2565787df8ae3a8126359e8cd6fafTrond Norbye new_cookie = apr_pstrcat(r->pool, new_cookie, "; domain=",
7b046969a1b2565787df8ae3a8126359e8cd6fafTrond Norbye dcfg->cookie_domain,
7b046969a1b2565787df8ae3a8126359e8cd6fafTrond Norbye (dcfg->style == CT_COOKIE2
7b046969a1b2565787df8ae3a8126359e8cd6fafTrond Norbye ? "; version=1"
7b046969a1b2565787df8ae3a8126359e8cd6fafTrond Norbye : ""),
7b046969a1b2565787df8ae3a8126359e8cd6fafTrond Norbye NULL);
7b046969a1b2565787df8ae3a8126359e8cd6fafTrond Norbye }
7b046969a1b2565787df8ae3a8126359e8cd6fafTrond Norbye
7b046969a1b2565787df8ae3a8126359e8cd6fafTrond Norbye apr_table_setn(r->headers_out,
7b046969a1b2565787df8ae3a8126359e8cd6fafTrond Norbye (dcfg->style == CT_COOKIE2 ? "Set-Cookie2" : "Set-Cookie"),
7b046969a1b2565787df8ae3a8126359e8cd6fafTrond Norbye new_cookie);
7b046969a1b2565787df8ae3a8126359e8cd6fafTrond Norbye apr_table_setn(r->notes, "cookie", apr_pstrdup(r->pool, cookiebuf)); /* log first time */
7b046969a1b2565787df8ae3a8126359e8cd6fafTrond Norbye return;
7b046969a1b2565787df8ae3a8126359e8cd6fafTrond Norbye}
7b046969a1b2565787df8ae3a8126359e8cd6fafTrond Norbye
7b046969a1b2565787df8ae3a8126359e8cd6fafTrond Norbyestatic int spot_cookie(request_rec *r)
7b046969a1b2565787df8ae3a8126359e8cd6fafTrond Norbye{
7b046969a1b2565787df8ae3a8126359e8cd6fafTrond Norbye cookie_dir_rec *dcfg = ap_get_module_config(r->per_dir_config,
7b046969a1b2565787df8ae3a8126359e8cd6fafTrond Norbye &usertrack_module);
7b046969a1b2565787df8ae3a8126359e8cd6fafTrond Norbye const char *cookie;
7b046969a1b2565787df8ae3a8126359e8cd6fafTrond Norbye const char *value;
7b046969a1b2565787df8ae3a8126359e8cd6fafTrond Norbye
2e3c025fdd5908a27cc82eb1d5346368a8be4e0dJorgen Austvik if (!dcfg->enabled) {
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye return DECLINED;
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye }
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye
2dbc1835e0ae88ad102e2b9a85e5c7b5298b14b6Knut Anders Hatlen if ((cookie = apr_table_get(r->headers_in,
2e3c025fdd5908a27cc82eb1d5346368a8be4e0dJorgen Austvik (dcfg->style == CT_COOKIE2
d3d2404f9a49bf70b124053feabe666f85ef5361Knut Anders Hatlen ? "Cookie2"
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye : "Cookie"))))
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye if ((value = ap_strstr_c(cookie, dcfg->cookie_name))) {
9cf297d9a579835e9336d587eaee187ca0954767Knut Anders Hatlen char *cookiebuf, *cookieend;
2dbc1835e0ae88ad102e2b9a85e5c7b5298b14b6Knut Anders Hatlen
2dbc1835e0ae88ad102e2b9a85e5c7b5298b14b6Knut Anders Hatlen value += strlen(dcfg->cookie_name) + 1; /* Skip over the '=' */
9cf297d9a579835e9336d587eaee187ca0954767Knut Anders Hatlen cookiebuf = apr_pstrdup(r->pool, value);
2dbc1835e0ae88ad102e2b9a85e5c7b5298b14b6Knut Anders Hatlen cookieend = strchr(cookiebuf, ';');
2dbc1835e0ae88ad102e2b9a85e5c7b5298b14b6Knut Anders Hatlen if (cookieend)
2dbc1835e0ae88ad102e2b9a85e5c7b5298b14b6Knut Anders Hatlen *cookieend = '\0'; /* Ignore anything after a ; */
2dbc1835e0ae88ad102e2b9a85e5c7b5298b14b6Knut Anders Hatlen
2dbc1835e0ae88ad102e2b9a85e5c7b5298b14b6Knut Anders Hatlen /* Set the cookie in a note, for logging */
5a0c5ad4116f5a4dd0dd5a0a4e6d02973cd5eef9Lubos Kosco apr_table_setn(r->notes, "cookie", cookiebuf);
2dbc1835e0ae88ad102e2b9a85e5c7b5298b14b6Knut Anders Hatlen
2dbc1835e0ae88ad102e2b9a85e5c7b5298b14b6Knut Anders Hatlen return DECLINED; /* There's already a cookie, no new one */
2dbc1835e0ae88ad102e2b9a85e5c7b5298b14b6Knut Anders Hatlen }
2dbc1835e0ae88ad102e2b9a85e5c7b5298b14b6Knut Anders Hatlen make_cookie(r);
2dbc1835e0ae88ad102e2b9a85e5c7b5298b14b6Knut Anders Hatlen return OK; /* We set our cookie */
2dbc1835e0ae88ad102e2b9a85e5c7b5298b14b6Knut Anders Hatlen}
2dbc1835e0ae88ad102e2b9a85e5c7b5298b14b6Knut Anders Hatlen
9cf297d9a579835e9336d587eaee187ca0954767Knut Anders Hatlenstatic void *make_cookie_log_state(apr_pool_t *p, server_rec *s)
9cf297d9a579835e9336d587eaee187ca0954767Knut Anders Hatlen{
9cf297d9a579835e9336d587eaee187ca0954767Knut Anders Hatlen cookie_log_state *cls =
9cf297d9a579835e9336d587eaee187ca0954767Knut Anders Hatlen (cookie_log_state *) apr_palloc(p, sizeof(cookie_log_state));
2dbc1835e0ae88ad102e2b9a85e5c7b5298b14b6Knut Anders Hatlen
9cf297d9a579835e9336d587eaee187ca0954767Knut Anders Hatlen cls->expires = 0;
2dbc1835e0ae88ad102e2b9a85e5c7b5298b14b6Knut Anders Hatlen
2dbc1835e0ae88ad102e2b9a85e5c7b5298b14b6Knut Anders Hatlen return (void *) cls;
2dbc1835e0ae88ad102e2b9a85e5c7b5298b14b6Knut Anders Hatlen}
2dbc1835e0ae88ad102e2b9a85e5c7b5298b14b6Knut Anders Hatlen
2dbc1835e0ae88ad102e2b9a85e5c7b5298b14b6Knut Anders Hatlenstatic void *make_cookie_dir(apr_pool_t *p, char *d)
f53d230f3e516218f38a7f1f2866e42bb3e0290eKnut Anders Hatlen{
f53d230f3e516218f38a7f1f2866e42bb3e0290eKnut Anders Hatlen cookie_dir_rec *dcfg;
2dbc1835e0ae88ad102e2b9a85e5c7b5298b14b6Knut Anders Hatlen
2dbc1835e0ae88ad102e2b9a85e5c7b5298b14b6Knut Anders Hatlen dcfg = (cookie_dir_rec *) apr_pcalloc(p, sizeof(cookie_dir_rec));
2dbc1835e0ae88ad102e2b9a85e5c7b5298b14b6Knut Anders Hatlen dcfg->cookie_name = COOKIE_NAME;
7b046969a1b2565787df8ae3a8126359e8cd6fafTrond Norbye dcfg->cookie_domain = NULL;
2dbc1835e0ae88ad102e2b9a85e5c7b5298b14b6Knut Anders Hatlen dcfg->style = CT_UNSET;
2dbc1835e0ae88ad102e2b9a85e5c7b5298b14b6Knut Anders Hatlen dcfg->enabled = 0;
2dbc1835e0ae88ad102e2b9a85e5c7b5298b14b6Knut Anders Hatlen return dcfg;
2dbc1835e0ae88ad102e2b9a85e5c7b5298b14b6Knut Anders Hatlen}
2dbc1835e0ae88ad102e2b9a85e5c7b5298b14b6Knut Anders Hatlen
2dbc1835e0ae88ad102e2b9a85e5c7b5298b14b6Knut Anders Hatlenstatic const char *set_cookie_enable(cmd_parms *cmd, void *mconfig, int arg)
2dbc1835e0ae88ad102e2b9a85e5c7b5298b14b6Knut Anders Hatlen{
9661674ed58ba62a40e43d1a4b38d5e77c3c6545Knut Anders Hatlen cookie_dir_rec *dcfg = mconfig;
9661674ed58ba62a40e43d1a4b38d5e77c3c6545Knut Anders Hatlen
9661674ed58ba62a40e43d1a4b38d5e77c3c6545Knut Anders Hatlen dcfg->enabled = arg;
2dbc1835e0ae88ad102e2b9a85e5c7b5298b14b6Knut Anders Hatlen return NULL;
2dbc1835e0ae88ad102e2b9a85e5c7b5298b14b6Knut Anders Hatlen}
9cf297d9a579835e9336d587eaee187ca0954767Knut Anders Hatlen
9cf297d9a579835e9336d587eaee187ca0954767Knut Anders Hatlenstatic const char *set_cookie_exp(cmd_parms *parms, void *dummy,
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye const char *arg)
9cf297d9a579835e9336d587eaee187ca0954767Knut Anders Hatlen{
c0550b01024b910b8c1468811c0ea663b10b1372Trond Norbye cookie_log_state *cls;
c0550b01024b910b8c1468811c0ea663b10b1372Trond Norbye time_t factor, modifier = 0;
c0550b01024b910b8c1468811c0ea663b10b1372Trond Norbye time_t num = 0;
c0550b01024b910b8c1468811c0ea663b10b1372Trond Norbye char *word;
20a0bde399487a651cdeb66fc8b44b2212036355Trond Norbye
c0550b01024b910b8c1468811c0ea663b10b1372Trond Norbye cls = ap_get_module_config(parms->server->module_config,
523201f786f6b12b7cf54091c6e5be167878cbeeTrond Norbye &usertrack_module);
523201f786f6b12b7cf54091c6e5be167878cbeeTrond Norbye /* The simple case first - all numbers (we assume) */
523201f786f6b12b7cf54091c6e5be167878cbeeTrond Norbye if (apr_isdigit(arg[0]) && apr_isdigit(arg[strlen(arg) - 1])) {
523201f786f6b12b7cf54091c6e5be167878cbeeTrond Norbye cls->expires = atol(arg);
523201f786f6b12b7cf54091c6e5be167878cbeeTrond Norbye return NULL;
523201f786f6b12b7cf54091c6e5be167878cbeeTrond Norbye }
523201f786f6b12b7cf54091c6e5be167878cbeeTrond Norbye
523201f786f6b12b7cf54091c6e5be167878cbeeTrond Norbye /*
c0550b01024b910b8c1468811c0ea663b10b1372Trond Norbye * The harder case - stolen from mod_expires
0a0811923cbbd2976425db6f4c78eed811c2825bKnut Anders Hatlen *
0a0811923cbbd2976425db6f4c78eed811c2825bKnut Anders Hatlen * CookieExpires "[plus] {<num> <type>}*"
0a0811923cbbd2976425db6f4c78eed811c2825bKnut Anders Hatlen */
c67bbf00f725074786ac47867d2276e7b935b570Trond Norbye
0a0811923cbbd2976425db6f4c78eed811c2825bKnut Anders Hatlen word = ap_getword_conf(parms->pool, &arg);
0a0811923cbbd2976425db6f4c78eed811c2825bKnut Anders Hatlen if (!strncasecmp(word, "plus", 1)) {
0a0811923cbbd2976425db6f4c78eed811c2825bKnut Anders Hatlen word = ap_getword_conf(parms->pool, &arg);
0a0811923cbbd2976425db6f4c78eed811c2825bKnut Anders Hatlen };
0a0811923cbbd2976425db6f4c78eed811c2825bKnut Anders Hatlen
a07b2874263e3c5f0cd2e83441719415d53059c2Knut Anders Hatlen /* {<num> <type>}* */
a07b2874263e3c5f0cd2e83441719415d53059c2Knut Anders Hatlen while (word[0]) {
a07b2874263e3c5f0cd2e83441719415d53059c2Knut Anders Hatlen /* <num> */
a07b2874263e3c5f0cd2e83441719415d53059c2Knut Anders Hatlen if (apr_isdigit(word[0]))
a07b2874263e3c5f0cd2e83441719415d53059c2Knut Anders Hatlen num = atoi(word);
a07b2874263e3c5f0cd2e83441719415d53059c2Knut Anders Hatlen else
a07b2874263e3c5f0cd2e83441719415d53059c2Knut Anders Hatlen return "bad expires code, numeric value expected.";
a07b2874263e3c5f0cd2e83441719415d53059c2Knut Anders Hatlen
a07b2874263e3c5f0cd2e83441719415d53059c2Knut Anders Hatlen /* <type> */
a07b2874263e3c5f0cd2e83441719415d53059c2Knut Anders Hatlen word = ap_getword_conf(parms->pool, &arg);
a07b2874263e3c5f0cd2e83441719415d53059c2Knut Anders Hatlen if (!word[0])
cdf37a6f2e4e7ca653ef8e791cc8e720fa148a39Peter Bray return "bad expires code, missing <type>";
a07b2874263e3c5f0cd2e83441719415d53059c2Knut Anders Hatlen
a07b2874263e3c5f0cd2e83441719415d53059c2Knut Anders Hatlen factor = 0;
a07b2874263e3c5f0cd2e83441719415d53059c2Knut Anders Hatlen if (!strncasecmp(word, "years", 1))
a07b2874263e3c5f0cd2e83441719415d53059c2Knut Anders Hatlen factor = 60 * 60 * 24 * 365;
a07b2874263e3c5f0cd2e83441719415d53059c2Knut Anders Hatlen else if (!strncasecmp(word, "months", 2))
a07b2874263e3c5f0cd2e83441719415d53059c2Knut Anders Hatlen factor = 60 * 60 * 24 * 30;
a07b2874263e3c5f0cd2e83441719415d53059c2Knut Anders Hatlen else if (!strncasecmp(word, "weeks", 1))
a07b2874263e3c5f0cd2e83441719415d53059c2Knut Anders Hatlen factor = 60 * 60 * 24 * 7;
a07b2874263e3c5f0cd2e83441719415d53059c2Knut Anders Hatlen else if (!strncasecmp(word, "days", 1))
a07b2874263e3c5f0cd2e83441719415d53059c2Knut Anders Hatlen factor = 60 * 60 * 24;
a07b2874263e3c5f0cd2e83441719415d53059c2Knut Anders Hatlen else if (!strncasecmp(word, "hours", 1))
dce551c28e0e9e0a652e512c1094caef574916ceKnut Anders Hatlen factor = 60 * 60;
a07b2874263e3c5f0cd2e83441719415d53059c2Knut Anders Hatlen else if (!strncasecmp(word, "minutes", 2))
a07b2874263e3c5f0cd2e83441719415d53059c2Knut Anders Hatlen factor = 60;
a07b2874263e3c5f0cd2e83441719415d53059c2Knut Anders Hatlen else if (!strncasecmp(word, "seconds", 1))
a07b2874263e3c5f0cd2e83441719415d53059c2Knut Anders Hatlen factor = 1;
edcb01bf549171673fd0bb4239f2edfc7a810397Knut Anders Hatlen else
edcb01bf549171673fd0bb4239f2edfc7a810397Knut Anders Hatlen return "bad expires code, unrecognized type";
edcb01bf549171673fd0bb4239f2edfc7a810397Knut Anders Hatlen
edcb01bf549171673fd0bb4239f2edfc7a810397Knut Anders Hatlen modifier = modifier + factor * num;
edcb01bf549171673fd0bb4239f2edfc7a810397Knut Anders Hatlen
edcb01bf549171673fd0bb4239f2edfc7a810397Knut Anders Hatlen /* next <num> */
edcb01bf549171673fd0bb4239f2edfc7a810397Knut Anders Hatlen word = ap_getword_conf(parms->pool, &arg);
edcb01bf549171673fd0bb4239f2edfc7a810397Knut Anders Hatlen }
edcb01bf549171673fd0bb4239f2edfc7a810397Knut Anders Hatlen
85e0595857351c6e22f75b8928967d14cb679ac5Jorgen Austvik cls->expires = modifier;
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik return NULL;
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik}
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik
c7eb123c8b2081a261deff3c401fbf92ddba1b58Jorgen Austvikstatic const char *set_cookie_name(cmd_parms *cmd, void *mconfig,
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik const char *name)
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik{
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik cookie_dir_rec *dcfg = (cookie_dir_rec *) mconfig;
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik
c7eb123c8b2081a261deff3c401fbf92ddba1b58Jorgen Austvik dcfg->cookie_name = apr_pstrdup(cmd->pool, name);
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik return NULL;
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik}
c7eb123c8b2081a261deff3c401fbf92ddba1b58Jorgen Austvik
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik/*
c7eb123c8b2081a261deff3c401fbf92ddba1b58Jorgen Austvik * Set the value for the 'Domain=' attribute.
c7eb123c8b2081a261deff3c401fbf92ddba1b58Jorgen Austvik */
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvikstatic const char *set_cookie_domain(cmd_parms *cmd, void *mconfig,
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik const char *name)
85e0595857351c6e22f75b8928967d14cb679ac5Jorgen Austvik{
c7eb123c8b2081a261deff3c401fbf92ddba1b58Jorgen Austvik cookie_dir_rec *dcfg;
85e0595857351c6e22f75b8928967d14cb679ac5Jorgen Austvik
85e0595857351c6e22f75b8928967d14cb679ac5Jorgen Austvik dcfg = (cookie_dir_rec *) mconfig;
85e0595857351c6e22f75b8928967d14cb679ac5Jorgen Austvik
85e0595857351c6e22f75b8928967d14cb679ac5Jorgen Austvik /*
85e0595857351c6e22f75b8928967d14cb679ac5Jorgen Austvik * Apply the restrictions on cookie domain attributes.
85e0595857351c6e22f75b8928967d14cb679ac5Jorgen Austvik */
85e0595857351c6e22f75b8928967d14cb679ac5Jorgen Austvik if (strlen(name) == 0) {
85e0595857351c6e22f75b8928967d14cb679ac5Jorgen Austvik return "CookieDomain values may not be null";
85e0595857351c6e22f75b8928967d14cb679ac5Jorgen Austvik }
2e3c025fdd5908a27cc82eb1d5346368a8be4e0dJorgen Austvik if (name[0] != '.') {
c7eb123c8b2081a261deff3c401fbf92ddba1b58Jorgen Austvik return "CookieDomain values must begin with a dot";
1f17ba9e3c026d75f488227451416bd72a222afeTrond Norbye }
c7eb123c8b2081a261deff3c401fbf92ddba1b58Jorgen Austvik if (ap_strchr_c(&name[1], '.') == NULL) {
c7eb123c8b2081a261deff3c401fbf92ddba1b58Jorgen Austvik return "CookieDomain values must contain at least one embedded dot";
1f17ba9e3c026d75f488227451416bd72a222afeTrond Norbye }
1f17ba9e3c026d75f488227451416bd72a222afeTrond Norbye
1f17ba9e3c026d75f488227451416bd72a222afeTrond Norbye dcfg->cookie_domain = apr_pstrdup(cmd->pool, name);
c7eb123c8b2081a261deff3c401fbf92ddba1b58Jorgen Austvik return NULL;
1f17ba9e3c026d75f488227451416bd72a222afeTrond Norbye}
1f17ba9e3c026d75f488227451416bd72a222afeTrond Norbye
1f17ba9e3c026d75f488227451416bd72a222afeTrond Norbye/*
1f17ba9e3c026d75f488227451416bd72a222afeTrond Norbye * Make a note of the cookie style we should use.
1f17ba9e3c026d75f488227451416bd72a222afeTrond Norbye */
1f17ba9e3c026d75f488227451416bd72a222afeTrond Norbyestatic const char *set_cookie_style(cmd_parms *cmd, void *mconfig,
1f17ba9e3c026d75f488227451416bd72a222afeTrond Norbye const char *name)
1f17ba9e3c026d75f488227451416bd72a222afeTrond Norbye{
1f17ba9e3c026d75f488227451416bd72a222afeTrond Norbye cookie_dir_rec *dcfg;
49f592091468eac515dde6139fbc8efa26056b0aJorgen Austvik
49f592091468eac515dde6139fbc8efa26056b0aJorgen Austvik dcfg = (cookie_dir_rec *) mconfig;
49f592091468eac515dde6139fbc8efa26056b0aJorgen Austvik
49f592091468eac515dde6139fbc8efa26056b0aJorgen Austvik if (strcasecmp(name, "Netscape") == 0) {
49f592091468eac515dde6139fbc8efa26056b0aJorgen Austvik dcfg->style = CT_NETSCAPE;
49f592091468eac515dde6139fbc8efa26056b0aJorgen Austvik }
945f4c3c36a15447913781dfb1894b34f2941c57Jorgen Austvik else if ((strcasecmp(name, "Cookie") == 0)
49f592091468eac515dde6139fbc8efa26056b0aJorgen Austvik || (strcasecmp(name, "RFC2109") == 0)) {
49f592091468eac515dde6139fbc8efa26056b0aJorgen Austvik dcfg->style = CT_COOKIE;
49f592091468eac515dde6139fbc8efa26056b0aJorgen Austvik }
45cf976b01987ad147e9edb54ffab0938dd6c016Trond Norbye else if ((strcasecmp(name, "Cookie2") == 0)
49f592091468eac515dde6139fbc8efa26056b0aJorgen Austvik || (strcasecmp(name, "RFC2965") == 0)) {
49f592091468eac515dde6139fbc8efa26056b0aJorgen Austvik dcfg->style = CT_COOKIE2;
49f592091468eac515dde6139fbc8efa26056b0aJorgen Austvik }
49f592091468eac515dde6139fbc8efa26056b0aJorgen Austvik else {
49f592091468eac515dde6139fbc8efa26056b0aJorgen Austvik return apr_psprintf(cmd->pool, "Invalid %s keyword: '%s'",
945f4c3c36a15447913781dfb1894b34f2941c57Jorgen Austvik cmd->cmd->name, name);
945f4c3c36a15447913781dfb1894b34f2941c57Jorgen Austvik }
945f4c3c36a15447913781dfb1894b34f2941c57Jorgen Austvik
945f4c3c36a15447913781dfb1894b34f2941c57Jorgen Austvik return NULL;
945f4c3c36a15447913781dfb1894b34f2941c57Jorgen Austvik}
945f4c3c36a15447913781dfb1894b34f2941c57Jorgen Austvik
945f4c3c36a15447913781dfb1894b34f2941c57Jorgen Austvikstatic const command_rec cookie_log_cmds[] = {
945f4c3c36a15447913781dfb1894b34f2941c57Jorgen Austvik AP_INIT_TAKE1("CookieExpires", set_cookie_exp, NULL, OR_FILEINFO,
945f4c3c36a15447913781dfb1894b34f2941c57Jorgen Austvik "an expiry date code"),
945f4c3c36a15447913781dfb1894b34f2941c57Jorgen Austvik AP_INIT_TAKE1("CookieDomain", set_cookie_domain, NULL, OR_FILEINFO,
945f4c3c36a15447913781dfb1894b34f2941c57Jorgen Austvik "domain to which this cookie applies"),
945f4c3c36a15447913781dfb1894b34f2941c57Jorgen Austvik AP_INIT_TAKE1("CookieStyle", set_cookie_style, NULL, OR_FILEINFO,
945f4c3c36a15447913781dfb1894b34f2941c57Jorgen Austvik "'Netscape', 'Cookie' (RFC2109), or 'Cookie2' (RFC2965)"),
945f4c3c36a15447913781dfb1894b34f2941c57Jorgen Austvik AP_INIT_FLAG("CookieTracking", set_cookie_enable, NULL, OR_FILEINFO,
945f4c3c36a15447913781dfb1894b34f2941c57Jorgen Austvik "whether or not to enable cookies"),
945f4c3c36a15447913781dfb1894b34f2941c57Jorgen Austvik AP_INIT_TAKE1("CookieName", set_cookie_name, NULL, OR_FILEINFO,
1e13442ac5687540ace78411d98155fca4b3791eKnut Anders Hatlen "name of the tracking cookie"),
945f4c3c36a15447913781dfb1894b34f2941c57Jorgen Austvik {NULL}
945f4c3c36a15447913781dfb1894b34f2941c57Jorgen Austvik};
945f4c3c36a15447913781dfb1894b34f2941c57Jorgen Austvik
945f4c3c36a15447913781dfb1894b34f2941c57Jorgen Austvikstatic void register_hooks(apr_pool_t *p)
2ef63dc9adc693ddad2206d323b0a85a1afe70ecJorgen Austvik{
945f4c3c36a15447913781dfb1894b34f2941c57Jorgen Austvik ap_hook_fixups(spot_cookie,NULL,NULL,APR_HOOK_MIDDLE);
1f17ba9e3c026d75f488227451416bd72a222afeTrond Norbye}
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik
f09d46eefeb5e4db6dc11e02e417b448fa9362a9Jorgen Austvikmodule AP_MODULE_DECLARE_DATA usertrack_module = {
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik STANDARD20_MODULE_STUFF,
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik make_cookie_dir, /* dir config creater */
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik NULL, /* dir merger --- default is to override */
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik make_cookie_log_state, /* server config */
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik NULL, /* merge server configs */
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik cookie_log_cmds, /* command apr_table_t */
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik register_hooks /* register hooks */
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik};
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik