#############################################################################
# Copyright (c) 2013 Balabit
# Copyright (c) 2013 Márton Illés <marci@balabit.com>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 as published
# by the Free Software Foundation, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# As an additional exemption you are allowed to compile & link against the
# OpenSSL libraries as published by the OpenSSL project. See the file
# COPYING for details.
#
#############################################################################
#
# The rewrite rules below can be used to mask out or hash credit card
# numbers in log messages.
#
# Their usage is simple:
#
# log {
#  source (s_local);
#  rewrite { credit-card-hash(); }; # or credit-card-mask();
#  destination (d_local);
# };
#
# For more information about the topic, see the following blog post:
#  http://marci.blogs.balabit.com/2013/02/masking-credit-card-numbers-in-log-messages-with-syslog-ng/
#
# A notable difference compared to the blog post, is that the hash_cc
# rule is called credit-card-hash and mask_cc is credit-card-mask.

block rewrite credit-card-internal(regex() value() template())
{
   subst("`regex`"
         "`template`"
         value(`value`)
         flags(global, store-matches)
         type(pcre)
    );
};

block rewrite credit-card-visa(value() template())
{
  credit-card-internal(regex("(?:4[0-9]{12}(?:[0-9]{3})?)") value(`value`) template(`template`));
};

block rewrite credit-card-mastercard(value() template())
{
  credit-card-internal(regex("((5[1-5][0-9]{14})|((?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}))") value(`value`) template(`template`));
};

block rewrite credit-card-american_express(value() template())
{
  credit-card-internal(regex("(3[47][0-9]{13})") value(`value`) template(`template`));
};

block rewrite credit-card-diners_club(value() template())
{
  credit-card-internal(regex("(3(?:0[0-5]|[68][0-9])[0-9]{11})") value(`value`) template(`template`));
};

block rewrite credit-card-discover(value() template())
{
  credit-card-internal(regex("(6(?:011|5[0-9]{2})[0-9]{12})") value(`value`) template(`template`));
};

block rewrite credit-card-jcb(value() template())
{
  credit-card-internal(regex("((?:2131|1800|35[0-9]{3})[0-9]{11})") value(`value`) template(`template`));
};


block rewrite credit-card-mask(value("MESSAGE") template("$(substr $0 0 6)******$(substr $0 -4 4)"))
{
    credit-card-visa(value(`value`) template("`template`"));
    credit-card-mastercard(value(`value`) template("`template`"));
    credit-card-american_express(value(`value`) template("`template`"));
    credit-card-jcb(value(`value`) template("`template`"));
    credit-card-diners_club(value(`value`) template("`template`"));
    credit-card-discover(value(`value`) template("`template`"));
};

block rewrite credit-card-hash(value("MESSAGE") template("$(sha1 --length 16 $0)"))
{
    credit-card-visa(value(`value`) template("`template`"));
    credit-card-mastercard(value(`value`) template("`template`"));
    credit-card-american_express(value(`value`) template("`template`"));
    credit-card-jcb(value(`value`) template("`template`"));
    credit-card-diners_club(value(`value`) template("`template`"));
    credit-card-discover(value(`value`) template("`template`"));
};
