Debian Codegraph @

plugins/logging/syslog

raw
1 #!perl -w
2
3 =head1 NAME
4
5 syslog - Syslog logging plugin for qpsmtpd
6
7 =head1 DESCRIPTION
8
9 The syslog plugin for qpsmtpd passes qpsmtpd log messages into the standard
10 UNIX syslog facility, mapping qpsmtpd priorities to syslog priorities.
11
12 =head1 CONFIGURATION
13
14 To enable the logging plugin, add a line of this form to the qpsmtpd plugins
15 configuration file:
16
17 =over
18
19 logging/syslog [loglevel l] [priority p] [ident str] [facility f] [logsock t]
20
21 For example:
22
23 logging/syslog loglevel LOGINFO priority LOG_NOTICE
24
25 =back
26
27 The following optional configuration settings can be supplied:
28
29 =over
30
31 =item B<loglevel>
32
33 The internal log level below which messages will be logged. Priorities count
34 downward as follows:
35
36 =over
37
38 =item B<LOGDEBUG>
39
40 =item B<LOGINFO>
41
42 =item B<LOGNOTICE>
43
44 =item B<LOGWARN>
45
46 =item B<LOGERROR>
47
48 =item B<LOGCRIT>
49
50 =item B<LOGALERT>
51
52 =item B<LOGEMERG>
53
54 =back
55
56
57 =item B<priority>
58
59 Normally, log messages will be mapped from the above log levels into the
60 syslog(3) log levels of their corresponding names. This will cause various
61 messages to appear or not in syslog outputs according to your syslogd
62 configuration (typically /etc/syslog.conf). However, if the B<priority>
63 setting is used, all messages will be logged at that priority regardless of
64 what the original priority might have been.
65
66 =item B<ident>
67
68 The ident string that will be attached to messages logged via this plugin.
69 The default is 'qpsmtpd'.
70
71 =item B<facility>
72
73 The syslog facility to which logged mesages will be directed. See syslog(3)
74 for details. The default is LOG_MAIL.
75
76 =item B<logsock>
77
78 The syslog socket where messages should be sent via syslogsock(). The valid
79 options are 'udp', 'tcp', 'unix', 'stream' and 'console'. Not all are
80 available on all systems. See Sys::Syslog for details. The default is
81 the above list in that order. To select specific sockets, use a comma to
82 separate the types.
83
84 =over
85
86 logsock udp,unix
87 logsock stream
88
89 =back
90
91 =back
92
93 =head1 AUTHOR
94
95 Devin Carraway <[email protected]>
96 Peter Eisch <[email protected]> (logsock support)
97
98 =head1 LICENSE
99
100 Copyright (c) 2005, Devin Carraway.
101
102 This plugin is licensed under the same terms as the qpsmtpd package itself.
103 Please see the LICENSE file included with qpsmtpd for details.
104
105 =cut
106
107 use strict;
108 use warnings;
109
110 use Qpsmtpd::Constants;
111 use Sys::Syslog qw(:DEFAULT setlogsock);
112
113 sub register {
114 my ($self, $qp, @args) = @_;
115 my %args;
116
117 if (@args % 2 == 0) {
118 %args = @args;
119 }
120 else {
121 warn "Malformed arguments to syslog plugin";
122 return;
123 }
124
125 my $ident = 'qpsmtpd';
126 my $logopt = 'pid';
127 my $facility = 'LOG_MAIL';
128
129 $self->{_loglevel} = LOGWARN;
130
131 if ($args{loglevel}) {
132 if ($args{loglevel} =~ /^(\d+)$/) {
133 $self->{_loglevel} = $1;
134 }
135 elsif ($args{loglevel} =~ /^(LOG\w+)$/) {
136 $self->{_loglevel} = log_level($1) || LOGWARN;
137 }
138 }
139
140 if ($args{priority}) {
141 if ($args{priority} =~ /^(\d+|LOG\w+)$/) {
142 $self->{_priority} = $1;
143 }
144 }
145
146 if ($args{ident} && $args{ident} =~ /^([\w\-.]+)$/) {
147 $ident = $1;
148 }
149 if ($args{facility} && $args{facility} =~ /^(\w+)$/) {
150 $facility = $1;
151 }
152
153 if ($args{logsock}) {
154 my @logopt = split(/,/, $args{logsock});
155 setlogsock(@logopt);
156 }
157
158 unless (openlog $ident, $logopt, $facility) {
159 warn "Error opening syslog output";
160 return;
161 }
162 }
163
164 my %priorities_ = (
165 0 => 'LOG_EMERG',
166 1 => 'LOG_ALERT',
167 2 => 'LOG_CRIT',
168 3 => 'LOG_ERR',
169 4 => 'LOG_WARNING',
170 5 => 'LOG_NOTICE',
171 6 => 'LOG_INFO',
172 7 => 'LOG_DEBUG',
173 );
174
175 sub hook_logging {
176 my ($self, $transaction, $trace, $hook, $plugin, @log) = @_;
177
178 return DECLINED if $trace > $self->{_loglevel};
179 return DECLINED if defined $plugin and $plugin eq $self->plugin_name;
180
181 my $priority =
182 $self->{_priority} ? $self->{_priority} : $priorities_{$trace};
183
184 syslog $priority, '%s', join(' ', @log);
185 return DECLINED;
186 }
187