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

# Sympa - SYsteme de Multi-Postage Automatique
#
7
8
# Copyright 2017, 2018, 2021 The Sympa Community. See the
# AUTHORS.md file at the top-level directory of this distribution and at
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# <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;

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

30
use Conf;
31
use Sympa::Constants;
32
use Sympa::List;
33
use Sympa::Log;
34
use Sympa::Regexps;
35
36
37

my $log = Sympa::Log->instance;

IKEDA Soji's avatar
IKEDA Soji committed
38
39
# Sympa::Aliases is the proxy class of subclasses.
# The constructor may be overridden by _new() method.
40
sub new {
41
42
43
44
45
46
47
48
49
50
51
    my $class   = shift;
    my $type    = shift;
    my %options = @_;

    return undef unless $type;

    # Special cases:
    # - To disable aliases management, specify "none" as $type.
    # - "External" module is used for full path to program.
    # - However, "Template" module is used instead of obsoleted program
    #   alias_manager.pl.
IKEDA Soji's avatar
IKEDA Soji committed
52
    return $class->_new if $type eq 'none';
53
54
55
56
57
58

    if ($type eq Sympa::Constants::SBINDIR() . '/alias_manager.pl') {
        $type = 'Sympa::Aliases::Template';
    } elsif (0 == index $type, '/' and -x $type) {
        $options{program} = $type;
        $type = 'Sympa::Aliases::External';
59
60
    }

61
62
63
64
65
66
67
68
69
70
71
    # Returns appropriate subclasses.
    if ($type !~ /[^\w:]/) {
        $type = sprintf 'Sympa::Aliases::%s', $type unless $type =~ /::/;
        unless (eval sprintf('require %s', $type)
            and $type->isa('Sympa::Aliases')) {
            $log->syslog(
                'err', 'Unable to use %s module: %s',
                $type, $EVAL_ERROR || 'Not a Sympa::Aliases class'
            );
            return undef;
        }
IKEDA Soji's avatar
IKEDA Soji committed
72
        return $type->_new(%options);
73
74
75
76
77
    }

    return undef;
}

IKEDA Soji's avatar
IKEDA Soji committed
78
79
80
81
82
83
84
sub _new {
    my $class   = shift;
    my %options = @_;

    return bless {%options} => $class;
}

85
sub check {0}
86

87
sub add {0}
88

89
sub del {0}
90

91
92
93
94
95
# Check listname.
sub check_new_listname {
    my $listname = shift;
    my $robot_id = shift;

96
97
98
99
    unless (defined $listname and length $listname) {
        $log->syslog('err', 'No listname');
        return ('user', 'listname_needed');
    }
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121

    $listname = lc $listname;

    my $listname_re = Sympa::Regexps::listname();
    unless (defined $listname
        and $listname =~ /^$listname_re$/i
        and length $listname <= Sympa::Constants::LIST_LEN()) {
        $log->syslog('err', 'Incorrect listname %s', $listname);
        return ('user', 'incorrect_listname', {bad_listname => $listname});
    }

    my $regx = Conf::get_robot_conf($robot_id, 'list_check_regexp');
    if ($regx) {
        if ($listname =~ /^(\S+)-($regx)$/) {
            $log->syslog('err',
                'Incorrect listname %s matches one of service aliases',
                $listname);
            return ('user', 'listname_matches_aliases',
                {new_listname => $listname});
        }
    }

122
    # Avoid "sympa", "listmaster", "bounce" and "bounce+XXX".
123
    if (   $listname eq Conf::get_robot_conf($robot_id, 'email')
124
        or $listname eq Conf::get_robot_conf($robot_id, 'listmaster_email')
125
126
127
128
129
130
        or $listname eq Conf::get_robot_conf($robot_id, 'bounce_email_prefix')
        or 0 == index(
            $listname,
            Conf::get_robot_conf($robot_id, 'bounce_email_prefix') . '+'
        )
    ) {
131
132
133
134
135
136
137
        $log->syslog('err',
            'Incorrect listname %s matches one of service aliases',
            $listname);
        return ('user', 'listname_matches_aliases',
            {new_listname => $listname});
    }

138
139
140
    # Prevent to use prohibited listnames
    my $regex = '';
    if ($Conf::Conf{'prohibited_listnames_regex'}) {
141
142
        $regex = eval(sprintf 'qr(%s)',
            $Conf::Conf{'prohibited_listnames_regex'} // '');
143
144
    }
    if ($Conf::Conf{'prohibited_listnames'}) {
145
146
147
        foreach my $l (split ',', $Conf::Conf{'prohibited_listnames'}) {
            $l =~ s/([^\s\w\x80-\xff])/\\$1/g;
            $l =~ s/(\\.)/$1 eq "\\*" ? '.*' : $1/eg;
148
149
150
151
152
153
154
155
156
            $l = sprintf('^%s$', $l);

            if ($regex) {
                $regex .= '|' . $l;
            } else {
                $regex .= $l;
            }
        }
    }
157
    if ($regex && $listname =~ m/$regex/i) {
158
159
160
161
        $log->syslog('err', 'Prohibited "%s"', $listname);
        return ('user', 'prohibited_listname', {argument => $listname});
    }

162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
    # Check listname on SMTP server.
    my $aliases =
        Sympa::Aliases->new(Conf::get_robot_conf($robot_id, 'alias_manager'));
    my $res = $aliases->check($listname, $robot_id) if $aliases;
    unless (defined $res) {
        $log->syslog('err', 'Can\'t check list %.128s on %s',
            $listname, $robot_id);
        return ('intern');
    }

    # Check this listname doesn't exist already.
    if ($res or Sympa::List->new($listname, $robot_id, {'just_try' => 1})) {
        $log->syslog('err',
            'Could not create list %s: list on %s already exist',
            $listname, $robot_id);
        return ('user', 'list_already_exists', {new_listname => $listname});
    }

    return;
}

183
184
185
186
187
1;
__END__

=encoding utf-8

188
=head1 NAME
189
190
191

Sympa::Aliases - Base class for alias management

192
193
194
195
196
197
198
199
200
201
202
203
=head1 SYNOPSIS

  package Sympa::Aliases::FOO;
  
  use base qw(Sympa::Aliases);
  
  sub check { ... }
  sub add { ... }
  sub del { ... }
  
  1;

204
205
=head1 DESCRIPTION 

206
This module is the base class for subclasses to manage list aliases of Sympa.
207
208
209
210
211

=head2 Methods

=over

212
=item new ( $type, [ key =E<gt> value, ... ] )
213
214
215
216

I<Constructor>.
Creates new instance of L<Sympa::Aliases>.

217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
Returns one of appropriate subclasses according to $type:

=over

=item C<'none'>

No aliases management.

=item Full path to executable

Use external program to manage aliases.
See L<Sympa::Aliases::External>.

=item Name of subclass

Use a subclass C<Sympa::Aliases::I<name>> to manage aliases.

=back

For invalid types returns C<undef>.

IKEDA Soji's avatar
IKEDA Soji committed
238
239
240
241
242
Note:
For compatibility to the earlier versions of Sympa,
if a string C<SBINDIR/alias_manager.pl> was given as $type,
L<Sympa::Aliases::Template> subclass will be used.

243
244
245
Optional C<I<key> =E<gt> I<value>> pairs are included in the instance as
hash entries.

246
247
=item check ($listname, $robot_id)

248
249
I<Instance method>, I<overridable>.
Checks if the addresses of requested list exist already.
250
251
252
253
254
255
256
257

Parameters:

=over

=item $listname

Name of the list.
258
Mandatory.
259
260
261
262
263
264
265
266
267

=item $robot_id

List's robot.

=back

Returns:

268
269
270
271
272
True value if one of addresses exists.
C<0> if none found.
C<undef> if something wrong happened.

By default, this method always returns C<0>.
273
274
275

=item add ($list)

276
I<Instance method>, I<overridable>.
277
278
279
280
281
282
283
284
285
286
287
288
289
290
Installs aliases for the list $list.

Parameters:

=over

=item $list

An instance of L<Sympa::List>.

=back

Returns:

291
292
293
294
295
C<1> if installation succeeded.
C<0> if there were no aliases to be installed.
C<undef> if not applicable.

By default, this method always returns C<0>.
296
297
298

=item del ($list)

299
I<Instance method>, I<overridable>.
300
301
302
303
304
305
306
307
308
309
310
311
312
313
Removes aliases for the list $list.

Parameters:

=over

=item $list

An instance of L<Sympa::List>.

=back

Returns:

314
315
316
317
318
C<1> if removal succeeded.
C<0> if there were no aliases to be removed.
C<undef> if not applicable.

By default, this method always returns C<0>.
319
320
321

=back

322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
=head2 Function

=over

=item check_new_listname ( $listname, $robot )

I<Function>.
Checks if a new listname is allowed.

TBD.

Parameteres:

=over

=item $listname

A list name to be checked.

=item $robot

Robot context.

=back

Returns:

If check fails, an array including information of errors.
If it succeeds, empty array.

B<Note>:
This should be used to check name of list to be created.
Names of existing lists may not necessarily pass checks by this function.

This function was added on Sympa 6.2.37b.2.

=back

360
361
362
363
364
365
=head1 SEE ALSO

L<Sympa::Aliases::CheckSMTP>,
L<Sympa::Aliases::External>,
L<Sympa::Aliases::Template>.

366
367
368
369
370
371
372
373
374
=head1 HISTORY

F<alias_manager.pl> as a program to automate alias management appeared on
Sympa 3.1b.13.

L<Sympa::Aliases> module as an OO-based class appeared on Sympa 6.2.23b,
and it obsoleted F<alias_manager.pl>.

=cut