1#!/usr/bin/perl
2# Copyright (c) 2010 Sampo Kellomaki (sampo@iki.fi), All Rights Reserved.
3# This is confidential unpublished proprietary source code of the author.
4# NO WARRANTY, not even implied warranties. Contains trade secrets.
5# Distribution prohibited unless authorized in writing.
6# Licensed under Apache License 2.0, see file COPYING.
7# $Id$
8#
9# 17.2.2010, created --Sampo
10#
11# Web GUI CGI for exploring ZXID logs and audit trail
12#
13# CGI / QUERY_STRING variables
14#   c  $cmd    Command
15#   d  $dir    Path to ZXID config directory, e.g: /var/zxid/ or /var/zxid/idp
16#   e  $eid    Filter logs by Entity ID
17#   n  $nid    Filter logs by Name ID
18#   s  $sid    Filter logs by session ID
19
20$usage = <<USAGE;
21Web GUI CGI for exploring ZXID logs and audit trail
22Usage: http://localhost:8081/zxidexplo.pl?QUERY_STRING
23       ./zxidexplo.pl -a QUERY_STRING
24         -a Ascii mode
25USAGE
26    ;
27
28die $USAGE if $ARGV[0] =~ /^-[Hh?]/;
29$ascii = shift if $ARGV[0] eq '-a';
30syswrite STDOUT, "Content-Type: text/html\r\n\r\n" if !$ascii;
31
32$ENV{QUERY_STRING} ||= shift;
33$cgi = cgidec($ENV{QUERY_STRING});
34$cmd = $$cgi{'c'};
35$dir = $$cgi{'d'} || '/var/zxid/';
36$eid = $$cgi{'e'};
37$nid = $$cgi{'n'};
38$sid = $$cgi{'s'};
39
40sub cgidec {
41    my ($d) = @_;
42    my %qs;
43    for $nv (split '&', $d) {
44	($n, $v) = split '=', $nv, 2;
45	$qs{$n} = $v;
46    }
47    return \%qs;
48}
49
50sub uridec {
51    my ($val) = @_;
52    $val =~ s/\+/ /g;
53    $val =~ s/%([0-9a-f]{2})/chr(hex($1))/gsex;  # URI decode
54    return $val;
55}
56
57sub urienc {
58    my ($val) = @_;
59    $val =~ s/([^A-Za-z0-9.,_-])/sprintf("%%%02x",ord($1))/gsex; # URI enc
60    return $val;
61}
62
63sub read_log {
64    open LOG, "./zxlogview ${dir}pem/logsign-nopw-cert.pem ${dir}pem/logenc-nopw-cert.pem <${dir}log/act|"
65	or die "Cannot open log decoding pipe: $!";
66    $/ = "\n";
67    while ($line = <LOG>) {
68	# ----+ 104 PP - 20100217-151751.352 19700101-000000.501 -:- - - - -      zxcall N W GOTMD http://idp.tas3.eu/zxididp?o=B -
69	($pre, $len, $se, $sig, $ourts, $srcts, $ipport, $ent, $mid, $a7nid, $nid, $mm, $vvv, $res, $op, $para, @rest) = split /\s+/, $line;
70
71	syswrite STDOUT, "$ourts $op\n";
72    }
73    close LOG;
74}
75
76sub show_log {
77    print "<title>ZXID SP Log Explorer Log listing</title><link type=\"text/css\" rel=stylesheet href=\"explo.css\">\n<pre>\n";
78    read_log();
79    syswrite STDOUT, "</pre>";
80}
81
82sub readall {
83    my ($f) = @_;
84    my ($pkg, $srcfile, $line) = caller;
85    undef $/;         # Read all in, without breaking on lines
86    open F, "<$f" or die "$srcfile:$line: Cant read($f): $!";
87    binmode F;
88    my $x = <F>;
89    close F;
90    return $x;
91}
92
93sub show_templ {
94    my ($templ, $hr) = @_;
95    $templ = readall($templ);
96    $templ =~ s/!!(\w+)/$$hr{$1}/gs;
97    syswrite STDOUT, $templ;
98    exit;
99}
100
101show_templ("explo-main.html", $cgi);
102
103__END__
104