Template.pm 9.73 KB
Newer Older
1
2
3
4
5
6
7
#!--PERL--
# -*- indent-tabs-mode: nil; -*-
# vim:ft=perl:et:sw=4
# $Id$

# Sympa - SYsteme de Multi-Postage Automatique
#
8
9
# Copyright 2017, 2018 The Sympa Community. See the AUTHORS.md file at the
# top-level directory of this distribution and at
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# <https://github.com/sympa-community/sympa.git>.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, 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, see <http://www.gnu.org/licenses/>.

package Sympa::Aliases::Template;

use strict;
use warnings;
use English qw(-no_match_vars);

use Conf;
use Sympa::Constants;
use Sympa::Language;
use Sympa::LockedFile;
use Sympa::Log;
use Sympa::Template;

use base qw(Sympa::Aliases::CheckSMTP);

my $language = Sympa::Language->instance;
my $log      = Sympa::Log->instance;
my $alias_wrapper =
    Sympa::Constants::LIBEXECDIR . '/sympa_newaliases-wrapper';

sub _aliases {
    my $self = shift;
    my $list = shift;

    my $domain   = $list->{'domain'};
    my $listname = $list->{'name'};

    my $data = {
        'date' => $language->gettext_strftime('%d %b %Y', localtime time),
        'list' => {
            'name'   => $listname,
56
57
58
            'domain' => $domain,
            # Compat. < 6.2.32
            'host' => $domain,
59
60
61
62
        },
        'is_default_domain' => ($domain eq $Conf::Conf{'domain'}),
        'return_path_suffix' =>
            Conf::get_robot_conf($domain, 'return_path_suffix'),
63
64

        # No longer used by default.
Luc Didry's avatar
Luc Didry committed
65
66
        'robot'          => $domain,
        'default_domain' => $Conf::Conf{'domain'},
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
    };

    my $aliases_dump;
    my $template = Sympa::Template->new($domain);
    unless ($template->parse($data, 'list_aliases.tt2', \$aliases_dump)) {
        $log->syslog(
            'err',
            'Can\'t parse list_aliases.tt2: %s',
            $template->{last_error}
        );
        return;
    }

    my @aliases = split /\n/, $aliases_dump;
    unless (@aliases) {
        $log->syslog('err', 'No aliases defined');
        return;
    }
    return @aliases;
}

sub add {
    my $self = shift;
    my $list = shift;

    return 0
        if lc Conf::get_robot_conf($list->{'domain'}, 'sendmail_aliases') eq
Luc Didry's avatar
Luc Didry committed
94
        'none';
95

96
97
98
99
    my $alias_file =
           $self->{file}
        || Conf::get_robot_conf($list->{'domain'}, 'sendmail_aliases')
        || Sympa::Constants::SENDMAIL_ALIASES();
100
101
    # Create a lock
    my $lock_fh;
102
103
    unless ($lock_fh = Sympa::LockedFile->new($alias_file, 20, '+')) {
        $log->syslog('err', 'Can\'t lock %s', $alias_file);
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
        return undef;
    }

    my @aliases = $self->_aliases($list);
    return undef unless @aliases;

    ## Check existing aliases
    if (_already_defined($alias_file, @aliases)) {
        $log->syslog('err', 'Some alias already exist');
        return undef;
    }

    my $fh;
    unless (open $fh, '>>', $alias_file) {
        $log->syslog('err', 'Unable to append to %s: %m', $alias_file);
        return undef;
    }

    foreach (@aliases) {
        print $fh "$_\n";
    }
    close $fh;

    # Newaliases
    unless ($self->{file}) {
        system($alias_wrapper, '--domain=' . $list->{'domain'});
        if ($CHILD_ERROR == -1) {
            $log->syslog('err', 'Failed to execute sympa_newaliases: %m');
            return undef;
        } elsif ($CHILD_ERROR & 127) {
            $log->syslog(
                'err',
                'sympa_newaliases was terminated by signal %d',
                $CHILD_ERROR & 127
            );
            return undef;
        } elsif ($CHILD_ERROR) {
            $log->syslog(
                'err',
                'sympa_newaliases exited with status %d',
                $CHILD_ERROR >> 8
            );
            return undef;
        }
    }

    # Unlock
    $lock_fh->close;

    return 1;
}

sub del {
    my $self = shift;
    my $list = shift;

    return 0
        if lc Conf::get_robot_conf($list->{'domain'}, 'sendmail_aliases') eq
Luc Didry's avatar
Luc Didry committed
162
        'none';
163

164
165
166
167
    my $alias_file =
           $self->{file}
        || Conf::get_robot_conf($list->{'domain'}, 'sendmail_aliases')
        || Sympa::Constants::SENDMAIL_ALIASES();
168
169
    # Create a lock
    my $lock_fh;
170
171
    unless ($lock_fh = Sympa::LockedFile->new($alias_file, 20, '+')) {
        $log->syslog('err', 'Can\'t lock %s', $alias_file);
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
        return undef;
    }

    my @aliases = $self->_aliases($list);
    return undef unless @aliases;

    my $tmp_alias_file = $Conf::Conf{'tmpdir'} . '/sympa_aliases.' . time;

    my $ifh;
    unless (open $ifh, '<', $alias_file) {
        $log->syslog('err', 'Could not read %s: %m', $alias_file);
        return undef;
    }

    my $ofh;
    unless (open $ofh, '>', $tmp_alias_file) {
        $log->syslog('err', 'Could not create %s: %m', $tmp_alias_file);
        return undef;
    }

    my @deleted_lines;
    while (my $alias = <$ifh>) {
        my $left_side = '';
        $left_side = $1 if $alias =~ /^([^\s:]+)[\s:]/;

        my $to_be_deleted = 0;
        foreach my $new_alias (@aliases) {
            next unless ($new_alias =~ /^([^\s:]+)[\s:]/);
            my $new_left_side = $1;

            if ($left_side eq $new_left_side) {
                push @deleted_lines, $alias;
                $to_be_deleted = 1;
                last;
            }
        }
        unless ($to_be_deleted) {
            ## append to new aliases file
            print $ofh $alias;
        }
    }
    close $ifh;
    close $ofh;

    unless (@deleted_lines) {
        $log->syslog('err', 'No matching line in %s', $alias_file);
        return 0;
    }
    # Replace old aliases file.
    unless (open $ifh, '<', $tmp_alias_file) {
        $log->syslog('err', 'Could not read %s: %m', $tmp_alias_file);
        return undef;
    }
    unless (open $ofh, '>', $alias_file) {
        $log->syslog('err', 'Could not overwrite %s: %m', $alias_file);
        return undef;
    }
    print $ofh do { local $RS; <$ifh> };
    close $ofh;
    close $ifh;
    unlink $tmp_alias_file;

    # Newaliases
    unless ($self->{file}) {
        system($alias_wrapper, '--domain=' . $list->{'domain'});
        if ($CHILD_ERROR == -1) {
            $log->syslog('err', 'Failed to execute sympa_newaliases: %m');
            return undef;
        } elsif ($CHILD_ERROR & 127) {
            $log->syslog(
                'err',
                'sympa_newaliases was terminated by signal %d',
                $CHILD_ERROR & 127
            );
            return undef;
        } elsif ($CHILD_ERROR) {
            $log->syslog(
                'err',
                'sympa_newaliases exited with status %d',
                $CHILD_ERROR >> 8
            );
            return undef;
        }
    }

    # Unlock
    $lock_fh->close;

    return 1;
}

# Check if an alias is already defined.
# Old name: already_defined() in alias_manager.pl.
sub _already_defined {
    my $alias_file = shift;
    my @aliases    = @_;

    my $fh;
    unless (open $fh, '<', $alias_file) {
        $log->syslog('err', 'Could not read %s: %m', $alias_file);
        return undef;
    }

    my $ret = 0;
    while (my $alias = <$fh>) {
        # skip comment
        next if $alias =~ /^#/;
        $alias =~ /^([^\s:]+)[\s:]/;
        my $left_side = $1;
        next unless ($left_side);
        foreach (@aliases) {
            next unless ($_ =~ /^([^\s:]+)[\s:]/);
            my $new_left_side = $1;
            if ($left_side eq $new_left_side) {
                $log->syslog('info', 'Alias already defined: %s', $left_side);
                $ret++;
            }
        }
    }

    close $fh;
    return $ret;
}

1;
__END__

=encoding utf-8

=head1 NAME

Sympa::Aliases::Template -
Alias management: Aliases file based on template

=head1 SYNOPSIS

  use Sympa::Aliases;
  my $aliases = Sympa::Aliases->new('Template',
      [ file => '/path/to/file' ] );
  $aliases->check('listname', 'domain');
  $aliases->add($list);
  $aliases->del($list);

=head1 DESCRIPTION

L<Sympa::Aliases::Template> manages list aliases based on template
F<list_aliases.tt2>.

=head2 Methods

=over

=item check ( $listname, $domain )

See L<Sympa::Aliases::CheckSMTP>.

=item add ( $list )

=item del ( $list )

Adds or removes aliases of list $list.

If constructor was called with C<file> option, it will be used as aliases
file and F<sympa_newaliases> utility will not be executed.
Otherwise, value of C<sendmail_aliases> parameter will be used as aliases
337
file and F<sympa_newaliases> utility will be executed to update
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
alias database.
If C<sendmail_aliases> parameter is set to C<none>, aliases will never be
updated.

=back

=head2 Configuration parameters

=over

=item return_path_suffix

Suffix of list return address.

=item sendmail_aliases

Path of the file that contains all list related aliases.

=item tmpdir

A directory temporary files are placed.

=back

=head1 FILES

=over

=item F<$SYSCONFDIR/I<domain name>/list_aliases.tt2>

=item F<$SYSCONFDIR/list_aliases.tt2>

=item F<$DEFAULTDIR/list_aliases.tt2>

Template of aliases: Specific to a domain, global context and the default.

=item F<$SENDMAIL_ALIASES>

Default location of aliases file.

=item F<$SBINDIR/sympa_newaliases>

Auxiliary program to update alias database.

=back

=head1 SEE ALSO

L<Sympa::Aliases>,
L<Sympa::Aliases::CheckSMTP>,
IKEDA Soji's avatar
IKEDA Soji committed
388
L<sympa_newaliases(1)>.
389
390
391
392
393
394
395
396
397
398

=head1 HISTORY

F<alias_manager.pl> to manage aliases using template appeared on
Sympa 3.1b.13.

L<Sympa::Aliases::Template> module appeared on Sympa 6.2.23b,
and it obsoleted F<alias_manager(8)>.

=cut