11f53c19fSbluhm#!/usr/bin/perl 2*a0e989f6Sbluhm# $OpenBSD: syslogd.pl,v 1.11 2019/09/17 22:24:08 bluhm Exp $ 31f53c19fSbluhm 41f53c19fSbluhm# Copyright (c) 2010-2014 Alexander Bluhm <bluhm@openbsd.org> 51f53c19fSbluhm# 61f53c19fSbluhm# Permission to use, copy, modify, and distribute this software for any 71f53c19fSbluhm# purpose with or without fee is hereby granted, provided that the above 81f53c19fSbluhm# copyright notice and this permission notice appear in all copies. 91f53c19fSbluhm# 101f53c19fSbluhm# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 111f53c19fSbluhm# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 121f53c19fSbluhm# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 131f53c19fSbluhm# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 141f53c19fSbluhm# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 151f53c19fSbluhm# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 161f53c19fSbluhm# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 171f53c19fSbluhm 181f53c19fSbluhmuse strict; 191f53c19fSbluhmuse warnings; 201f53c19fSbluhmuse Socket; 211f53c19fSbluhmuse Socket6; 221f53c19fSbluhm 231f53c19fSbluhmuse Client; 241f53c19fSbluhmuse Syslogd; 251f53c19fSbluhmuse Server; 26aa580fcbSbluhmuse Syslogc; 27a0156d40Sbluhmuse RSyslogd; 281f53c19fSbluhmrequire 'funcs.pl'; 291f53c19fSbluhm 301f53c19fSbluhmsub usage { 311f53c19fSbluhm die "usage: syslogd.pl [test-args.pl]\n"; 321f53c19fSbluhm} 331f53c19fSbluhm 341f53c19fSbluhmmy $testfile; 351f53c19fSbluhmour %args; 361f53c19fSbluhmif (@ARGV and -f $ARGV[-1]) { 371f53c19fSbluhm $testfile = pop; 381f53c19fSbluhm do $testfile 391f53c19fSbluhm or die "Do test file $testfile failed: ", $@ || $!; 401f53c19fSbluhm} 411f53c19fSbluhm@ARGV == 0 or usage(); 421f53c19fSbluhm 4328157168Sbluhmcreate_multifile(@{$args{multifile} || []}); 44a0156d40Sbluhmforeach my $name (qw(client syslogd server rsyslogd)) { 45a0156d40Sbluhm $args{$name} or next; 461f53c19fSbluhm foreach my $action (qw(connect listen)) { 471f53c19fSbluhm my $h = $args{$name}{$action} or next; 489f5e6548Sbluhm defined $h->{domain} 499f5e6548Sbluhm or die "No domain specified in $name $action"; 509f5e6548Sbluhm foreach my $k (qw(domain proto addr port)) { 519f5e6548Sbluhm next unless defined $h->{$k}; 521f53c19fSbluhm $args{$name}{"$action$k"} = $h->{$k}; 531f53c19fSbluhm } 541f53c19fSbluhm } 551f53c19fSbluhm} 56f8f8530cSbluhmmy($s, $c, $r, $rc, @m); 57a0156d40Sbluhm$s = RSyslogd->new( 58a0156d40Sbluhm %{$args{rsyslogd}}, 59a0156d40Sbluhm listenport => scalar find_ports(%{$args{rsyslogd}{listen}}), 60a0156d40Sbluhm testfile => $testfile, 619f5e6548Sbluhm) if $args{rsyslogd}{listen} && !$args{rsyslogd}{connect}; 62a0156d40Sbluhm$s ||= Server->new( 631f53c19fSbluhm func => \&read_log, 641f53c19fSbluhm listendomain => AF_INET, 651f53c19fSbluhm listenaddr => "127.0.0.1", 661f53c19fSbluhm %{$args{server}}, 671f53c19fSbluhm testfile => $testfile, 68723ac965Sbluhm client => \$c, 69723ac965Sbluhm syslogd => \$r, 701f53c19fSbluhm) unless $args{server}{noserver}; 71aa580fcbSbluhm$args{syslogc} = [ $args{syslogc} ] if ref $args{syslogc} eq 'HASH'; 72aa580fcbSbluhmmy $i = 0; 73aa580fcbSbluhm@m = map { Syslogc->new( 74aa580fcbSbluhm %{$_}, 75aa580fcbSbluhm testfile => $testfile, 76aa580fcbSbluhm ktracefile => "syslogc-$i.ktrace", 77aa580fcbSbluhm logfile => "syslogc-".$i++.".log", 78aa580fcbSbluhm) } @{$args{syslogc}}; 79723ac965Sbluhm$r = Syslogd->new( 801f53c19fSbluhm connectaddr => "127.0.0.1", 811f53c19fSbluhm connectport => $s && $s->{listenport}, 82aa580fcbSbluhm ctlsock => @m && $m[0]->{ctlsock}, 831f53c19fSbluhm %{$args{syslogd}}, 841f53c19fSbluhm testfile => $testfile, 85723ac965Sbluhm client => \$c, 86723ac965Sbluhm server => \$s, 871f53c19fSbluhm); 88f8f8530cSbluhm$rc = RSyslogd->new( 899f5e6548Sbluhm %{$args{rsyslogd}}, 909f5e6548Sbluhm listenport => scalar find_ports(%{$args{rsyslogd}{listen}}), 919f5e6548Sbluhm testfile => $testfile, 929f5e6548Sbluhm) if $args{rsyslogd}{connect}; 93723ac965Sbluhm$c = Client->new( 941f53c19fSbluhm func => \&write_log, 959f5e6548Sbluhm connectport => $rc && $rc->{listenport}, 961f53c19fSbluhm %{$args{client}}, 971f53c19fSbluhm testfile => $testfile, 98723ac965Sbluhm syslogd => \$r, 99723ac965Sbluhm server => \$s, 1001f53c19fSbluhm) unless $args{client}{noclient}; 1019f5e6548Sbluhm($rc, $c) = ($c, $rc) if $rc; # chain client -> rsyslogd -> syslogd 1021f53c19fSbluhm 1034dc3bc25Sbluhmif (!$args{client}{noclient} && $c->{early}) { 1044dc3bc25Sbluhm $c->run->up; 1054dc3bc25Sbluhm $c->loggrep(get_firstlog(), 10) 1064dc3bc25Sbluhm or die ref($c), " no first log during early startup"; 1074dc3bc25Sbluhm} 108a0156d40Sbluhm$r->run unless $r->{late}; 109723ac965Sbluhm$s->run->up unless $args{server}{noserver}; 110a0156d40Sbluhm$r->run if $r->{late}; 1111f53c19fSbluhm$r->up; 112aa580fcbSbluhmmy $control = 0; 113aa580fcbSbluhmforeach (@m) { 114aa580fcbSbluhm if ($_->{early} || $_->{stop}) { 115aa580fcbSbluhm $_->run->up; 116aa580fcbSbluhm $control++; 117aa580fcbSbluhm } 118aa580fcbSbluhm} 119aa580fcbSbluhm$r->loggrep("Accepting control connection") if $control; 120aa580fcbSbluhmforeach (@m) { 121aa580fcbSbluhm if ($_->{stop}) { 122aa580fcbSbluhm $_->kill('STOP'); 123aa580fcbSbluhm } 124aa580fcbSbluhm} 125ab4b416dSbluhm$c->run->up if !$args{client}{noclient} && !$c->{early}; 1269f5e6548Sbluhm$rc->run->up if $args{rsyslogd}{connect}; 1271f53c19fSbluhm 128*a0e989f6Sbluhm$rc->down if $args{rsyslogd}{connect}; 129ab4b416dSbluhm$c->down if !$args{client}{noclient} && !$c->{early}; 1301f53c19fSbluhm$s->down unless $args{server}{noserver}; 131aa580fcbSbluhmforeach (@m) { 132aa580fcbSbluhm if ($_->{stop}) { 133aa580fcbSbluhm $_->kill('CONT'); 134aa580fcbSbluhm $_->down; 135aa580fcbSbluhm } elsif ($_->{early}) { 136aa580fcbSbluhm $_->down; 137aa580fcbSbluhm } else { 138aa580fcbSbluhm $_->run->up->down; 139aa580fcbSbluhm } 140aa580fcbSbluhm} 1411f53c19fSbluhm$r->kill_child; 1421f53c19fSbluhm$r->down; 143ab4b416dSbluhm$c->down if !$args{client}{noclient} && $c->{early}; 1441f53c19fSbluhm 145723ac965Sbluhm$args{check}->({client => $c, syslogd => $r, server => $s}) if $args{check}; 146ac8221f6Sbluhmcheck_logs($c, $r, $s, \@m, %args); 147