#!/usr/bin/perl

use strict;
use IO::Socket;
use IO::Socket::INET;
use Sys::Hostname;

use constant MONTH => qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );


my %opt = (
    prog => $0,
    destination => "127.0.0.1:2222",
    hostname => hostname,
    );

while(@ARGV) {
    my $arg = shift(@ARGV);
    if($arg =~ m/^--(prog|destination|hostname)=(.*)$/) {
	$opt{$1} = $2;
    } else {
	die(<<USAGE);
Usage: $0 [ --prog= ] [ --destination=127.0.0.1:2222 ] [ --hostname= ]
USAGE
    }
}


my $sock = new IO::Socket::INET(Proto => "udp",
                                PeerAddr => $opt{destination},
                                ReuseAddr => 1);
die("Sock: $!\n") unless($sock);

my $system = sprintf("%s %s[%d]",
		     $opt{hostname}, $opt{prog}, $$);

while($_ = <>) {
    chomp;
    my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
    # 36 => system daemon : Info
    my $hdr = sprintf("<36>%s %2d %02d:%02d:%02d %s: ",
		      (MONTH)[$mon], $mday, $hour, $min, $sec,
		      $system);
    $sock->send($hdr.$_);
}
$sock->close();


__END__

=pod

=head1  NAME

syslog-inject - injecting messages into syslog system

=head1 SYNOPSIS

syslog-inject [ --prog= ] [ --destination=ip:port ] [ --hostname= ]

=head1 DESCRIPTION

A tool to inject messages (read from STDIN) into the syslog system, for parsing by syslog-blacklist(1).

The options are as follows:

--prog= name for syslogging

--destination= where to send the messages (default: 127.0.0.1:2222)

--hostname= what host to claim the message originated on

=head1 SEE ALSO

ipset(1), iptables(1), syslog-blacklist(1)

=head1 AUTHOR

Morten Bogeskov <source.bogeskov.dk>
