aboutsummaryrefslogtreecommitdiffstats
path: root/Mailman/ListAdmin.py
blob: d083a5216b718d1586d3b6e6cf4f0ea64704e8c4 (plain) (blame)
1
2
3
4
5
6
7
8
9
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
56
57
58
59
60
61
62
63
64
65
66
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
94
95
96
97
98
99
100
101
102
103
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
162
163
164
165
166
167
168
169
170
171
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
337
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
# Copyright (C) 1998-2014 by the Free Software Foundation, Inc.
#
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

"""Mixin class for MailList which handles administrative requests.

Two types of admin requests are currently supported: adding members to a
closed or semi-closed list, and moderated posts.

Pending subscriptions which are requiring a user's confirmation are handled
elsewhere.
"""

import os
import time
import errno
import cPickle
import marshal
from cStringIO import StringIO

import email
from email.MIMEMessage import MIMEMessage
from email.Generator import Generator
from email.Utils import getaddresses

from Mailman import mm_cfg
from Mailman import Utils
from Mailman import Message
from Mailman import Errors
from Mailman.UserDesc import UserDesc
from Mailman.Queue.sbcache import get_switchboard
from Mailman.Logging.Syslog import syslog
from Mailman import i18n

_ = i18n._
D_ = i18n.D_

# Request types requiring admin approval
IGN = 0
HELDMSG = 1
SUBSCRIPTION = 2
UNSUBSCRIPTION = 3

# Return status from __handlepost()
DEFER = 0
REMOVE = 1
LOST = 2

DASH = '-'
NL = '\n'

try:
    True, False
except NameError:
    True = 1
    False = 0



class ListAdmin:
    def InitVars(self):
        # non-configurable data
        self.next_request_id = 1

    def InitTempVars(self):
        self.__db = None
        self.__filename = os.path.join(self.fullpath(), 'request.pck')

    def __opendb(self):
        if self.__db is None:
            assert self.Locked()
            try:
                fp = open(self.__filename)
                try:
                    self.__db = cPickle.load(fp)
                finally:
                    fp.close()
            except IOError, e:
                if e.errno <> errno.ENOENT: raise
                self.__db = {}
                # put version number in new database
                self.__db['version'] = IGN, mm_cfg.REQUESTS_FILE_SCHEMA_VERSION

    def __closedb(self):
        if self.__db is not None:
            assert self.Locked()
            # Save the version number
            self.__db['version'] = IGN, mm_cfg.REQUESTS_FILE_SCHEMA_VERSION
            # Now save a temp file and do the tmpfile->real file dance.  BAW:
            # should we be as paranoid as for the config.pck file?  Should we
            # use pickle?
            tmpfile = self.__filename + '.tmp'
            omask = os.umask(007)
            try:
                fp = open(tmpfile, 'w')
                try:
                    cPickle.dump(self.__db, fp, 1)
                    fp.flush()
                    os.fsync(fp.fileno())
                finally:
                    fp.close()
            finally:
                os.umask(omask)
            self.__db = None
            # Do the dance
            os.rename(tmpfile, self.__filename)

    def __nextid(self):
        assert self.Locked()
        while True:
            next = self.next_request_id
            self.next_request_id += 1
            if not self.__db.has_key(next):
                break
        return next

    def SaveRequestsDb(self):
        self.__closedb()

    def NumRequestsPending(self):
        self.__opendb()
        # Subtract one for the version pseudo-entry
        return len(self.__db) - 1

    def __getmsgids(self, rtype):
        self.__opendb()
        ids = [k for k, (op, data) in self.__db.items() if op == rtype]
        ids.sort()
        return ids

    def GetHeldMessageIds(self):
        return self.__getmsgids(HELDMSG)

    def GetSubscriptionIds(self):
        return self.__getmsgids(SUBSCRIPTION)

    def GetUnsubscriptionIds(self):
        return self.__getmsgids(UNSUBSCRIPTION)

    def GetRecord(self, id):
        self.__opendb()
        type, data = self.__db[id]
        return data

    def GetRecordType(self, id):
        self.__opendb()
        type, data = self.__db[id]
        return type

    def HandleRequest(self, id, value, comment=None, preserve=None,
                      forward=None, addr=None):
        self.__opendb()
        rtype, data = self.__db[id]
        if rtype == HELDMSG:
            status = self.__handlepost(data, value, comment, preserve,
                                       forward, addr)
        elif rtype == UNSUBSCRIPTION:
            status = self.__handleunsubscription(data, value, comment)
        else:
            assert rtype == SUBSCRIPTION
            status = self.__handlesubscription(data, value, comment)
        if status <> DEFER:
            # BAW: Held message ids are linked to Pending cookies, allowing
            # the user to cancel their post before the moderator has approved
            # it.  We should probably remove the cookie associated with this
            # id, but we have no way currently of correlating them. :(
            del self.__db[id]

    def HoldMessage(self, msg, reason, msgdata={}):
        # Make a copy of msgdata so that subsequent changes won't corrupt the
        # request database.  TBD: remove the `filebase' key since this will
        # not be relevant when the message is resurrected.
        msgdata = msgdata.copy()
        # assure that the database is open for writing
        self.__opendb()
        # get the next unique id
        id = self.__nextid()
        # get the message sender
        sender = msg.get_sender()
        # calculate the file name for the message text and write it to disk
        if mm_cfg.HOLD_MESSAGES_AS_PICKLES:
            ext = 'pck'
        else:
            ext = 'txt'
        filename = 'heldmsg-%s-%d.%s' % (self.internal_name(), id, ext)
        omask = os.umask(007)
        try:
            fp = open(os.path.join(mm_cfg.DATA_DIR, filename), 'w')
            try:
                if mm_cfg.HOLD_MESSAGES_AS_PICKLES:
                    cPickle.dump(msg, fp, 1)
                else:
                    g = Generator(fp)
                    g.flatten(msg, 1)
                fp.flush()
                os.fsync(fp.fileno())
            finally:
                fp.close()
        finally:
            os.umask(omask)
        # save the information to the request database.  for held message
        # entries, each record in the database will be of the following
        # format:
        #
        # the time the message was received
        # the sender of the message
        # the message's subject
        # a string description of the problem
        # name of the file in $PREFIX/data containing the msg text
        # an additional dictionary of message metadata
        #
        msgsubject = msg.get('subject', _('(no subject)'))
        if not sender:
            sender = _('<missing>')
        data = time.time(), sender, msgsubject, reason, filename, msgdata
        self.__db[id] = (HELDMSG, data)
        return id

    def __handlepost(self, record, value, comment, preserve, forward, addr):
        # For backwards compatibility with pre 2.0beta3
        ptime, sender, subject, reason, filename, msgdata = record
        path = os.path.join(mm_cfg.DATA_DIR, filename)
        # Handle message preservation
        if preserve:
            parts = os.path.split(path)[1].split(DASH)
            parts[0] = 'spam'
            spamfile = DASH.join(parts)
            # Preserve the message as plain text, not as a pickle
            try:
                fp = open(path)
            except IOError, e:
                if e.errno <> errno.ENOENT: raise
                return LOST
            try:
                if path.endswith('.pck'):
                    msg = cPickle.load(fp)
                else:
                    assert path.endswith('.txt'), '%s not .pck or .txt' % path
                    msg = fp.read()
            finally:
                fp.close()
            # Save the plain text to a .msg file, not a .pck file
            outpath = os.path.join(mm_cfg.SPAM_DIR, spamfile)
            head, ext = os.path.splitext(outpath)
            outpath = head + '.msg'
            outfp = open(outpath, 'w')
            try:
                if path.endswith('.pck'):
                    g = Generator(outfp)
                    g.flatten(msg, 1)
                else:
                    outfp.write(msg)
            finally:
                outfp.close()
        # Now handle updates to the database
        rejection = None
        fp = None
        msg = None
        status = REMOVE
        if value == mm_cfg.DEFER:
            # Defer
            status = DEFER
        elif value == mm_cfg.APPROVE:
            # Approved.
            try:
                msg = readMessage(path)
            except IOError, e:
                if e.errno <> errno.ENOENT: raise
                return LOST
            msg = readMessage(path)
            msgdata['approved'] = 1
            # adminapproved is used by the Emergency handler
            msgdata['adminapproved'] = 1
            # Calculate a new filebase for the approved message, otherwise
            # delivery errors will cause duplicates.
            try:
                del msgdata['filebase']
            except KeyError:
                pass
            # Queue the file for delivery by qrunner.  Trying to deliver the
            # message directly here can lead to a huge delay in web
            # turnaround.  Log the moderation and add a header.
            msg['X-Mailman-Approved-At'] = email.Utils.formatdate(localtime=1)
            syslog('vette', '%s: held message approved, message-id: %s',
                   self.internal_name(),
                   msg.get('message-id', 'n/a'))
            # Stick the message back in the incoming queue for further
            # processing.
            inq = get_switchboard(mm_cfg.INQUEUE_DIR)
            inq.enqueue(msg, _metadata=msgdata)
        elif value == mm_cfg.REJECT:
            # Rejected
            rejection = 'Refused'
            lang = self.getMemberLanguage(sender)
            subject = Utils.oneline(subject, Utils.GetCharSet(lang))
            self.__refuse(_('Posting of your message titled "%(subject)s"'),
                          sender, comment or _('[No reason given]'),
                          lang=lang)
        else:
            assert value == mm_cfg.DISCARD
            # Discarded
            rejection = 'Discarded'
        # Forward the message
        if forward and addr:
            # If we've approved the message, we need to be sure to craft a
            # completely unique second message for the forwarding operation,
            # since we don't want to share any state or information with the
            # normal delivery.
            try:
                copy = readMessage(path)
            except IOError, e:
                if e.errno <> errno.ENOENT: raise
                raise Errors.LostHeldMessage(path)
            # It's possible the addr is a comma separated list of addresses.
            addrs = getaddresses([addr])
            if len(addrs) == 1:
                realname, addr = addrs[0]
                # If the address getting the forwarded message is a member of
                # the list, we want the headers of the outer message to be
                # encoded in their language.  Otherwise it'll be the preferred
                # language of the mailing list.
                lang = self.getMemberLanguage(addr)
            else:
                # Throw away the realnames
                addr = [a for realname, a in addrs]
                # Which member language do we attempt to use?  We could use
                # the first match or the first address, but in the face of
                # ambiguity, let's just use the list's preferred language
                lang = self.preferred_language
            otrans = i18n.get_translation()
            i18n.set_language(lang)
            try:
                fmsg = Message.UserNotification(
                    addr, self.GetBouncesEmail(),
                    _('Forward of moderated message'),
                    lang=lang)
            finally:
                i18n.set_translation(otrans)
            fmsg.set_type('message/rfc822')
            fmsg.attach(copy)
            fmsg.send(self)
        # Log the rejection
        if rejection:
            note = '''%(listname)s: %(rejection)s posting:
\tFrom: %(sender)s
\tSubject: %(subject)s''' % {
                'listname' : self.internal_name(),
                'rejection': rejection,
                'sender'   : str(sender).replace('%', '%%'),
                'subject'  : str(subject).replace('%', '%%'),
                }
            if comment:
                note += '\n\tReason: ' + comment.replace('%', '%%')
            syslog('vette', note)
        # Always unlink the file containing the message text.  It's not
        # necessary anymore, regardless of the disposition of the message.
        if status <> DEFER:
            try:
                os.unlink(path)
            except OSError, e:
                if e.errno <> errno.ENOENT: raise
                # We lost the message text file.  Clean up our housekeeping
                # and inform of this status.
                return LOST
        return status

    def HoldSubscription(self, addr, fullname, password, digest, lang):
        # Assure that the database is open for writing
        self.__opendb()
        # Get the next unique id
        id = self.__nextid()
        # Save the information to the request database. for held subscription
        # entries, each record in the database will be one of the following
        # format:
        #
        # the time the subscription request was received
        # the subscriber's address
        # the subscriber's selected password (TBD: is this safe???)
        # the digest flag
        # the user's preferred language
        data = time.time(), addr, fullname, password, digest, lang
        self.__db[id] = (SUBSCRIPTION, data)
        #
        # TBD: this really shouldn't go here but I'm not sure where else is
        # appropriate.
        syslog('vette', '%s: held subscription request from %s',
               self.internal_name(), addr)
        # Possibly notify the administrator in default list language
        if self.admin_immed_notify:
            i18n.set_language(self.preferred_language)
            realname = self.real_name
            subject = _(
                'New subscription request to list %(realname)s from %(addr)s')
            text = Utils.maketext(
                'subauth.txt',
                {'username'   : addr,
                 'listname'   : self.internal_name(),
                 'hostname'   : self.host_name,
                 'admindb_url': self.GetScriptURL('admindb', absolute=1),
                 }, mlist=self)
            # This message should appear to come from the <list>-owner so as
            # to avoid any useless bounce processing.
            owneraddr = self.GetOwnerEmail()
            msg = Message.UserNotification(owneraddr, owneraddr, subject, text,
                                           self.preferred_language)
            msg.send(self, **{'tomoderators': 1})
            # Restore the user's preferred language.
            i18n.set_language(lang)

    def __handlesubscription(self, record, value, comment):
        stime, addr, fullname, password, digest, lang = record
        if value == mm_cfg.DEFER:
            return DEFER
        elif value == mm_cfg.DISCARD:
            syslog('vette', '%s: discarded subscription request from %s',
                   self.internal_name(), addr)
        elif value == mm_cfg.REJECT:
            self.__refuse(_('Subscription request'), addr,
                          comment or _('[No reason given]'),
                          lang=lang)
            syslog('vette', """%s: rejected subscription request from %s
\tReason: %s""", self.internal_name(), addr, comment or '[No reason given]')
        else:
            # subscribe
            assert value == mm_cfg.SUBSCRIBE
            try:
                userdesc = UserDesc(addr, fullname, password, digest, lang)
                self.ApprovedAddMember(userdesc,
                                       whence=D_('via admin approval'))
            except Errors.MMAlreadyAMember:
                # User has already been subscribed, after sending the request
                pass
            # TBD: disgusting hack: ApprovedAddMember() can end up closing
            # the request database.
            self.__opendb()
        return REMOVE

    def HoldUnsubscription(self, addr):
        # Assure the database is open for writing
        self.__opendb()
        # Get the next unique id
        id = self.__nextid()
        # All we need to do is save the unsubscribing address
        self.__db[id] = (UNSUBSCRIPTION, addr)
        syslog('vette', '%s: held unsubscription request from %s',
               self.internal_name(), addr)
        # Possibly notify the administrator of the hold
        if self.admin_immed_notify:
            realname = self.real_name
            subject = _(
                'New unsubscription request from %(realname)s by %(addr)s')
            text = Utils.maketext(
                'unsubauth.txt',
                {'username'   : addr,
                 'listname'   : self.internal_name(),
                 'hostname'   : self.host_name,
                 'admindb_url': self.GetScriptURL('admindb', absolute=1),
                 }, mlist=self)
            # This message should appear to come from the <list>-owner so as
            # to avoid any useless bounce processing.
            owneraddr = self.GetOwnerEmail()
            msg = Message.UserNotification(owneraddr, owneraddr, subject, text,
                                           self.preferred_language)
            msg.send(self, **{'tomoderators': 1})

    def __handleunsubscription(self, record, value, comment):
        addr = record
        if value == mm_cfg.DEFER:
            return DEFER
        elif value == mm_cfg.DISCARD:
            syslog('vette', '%s: discarded unsubscription request from %s',
                   self.internal_name(), addr)
        elif value == mm_cfg.REJECT:
            self.__refuse(_('Unsubscription request'), addr, comment)
            syslog('vette', """%s: rejected unsubscription request from %s
\tReason: %s""", self.internal_name(), addr, comment or '[No reason given]')
        else:
            assert value == mm_cfg.UNSUBSCRIBE
            try:
                self.ApprovedDeleteMember(addr)
            except Errors.NotAMemberError:
                # User has already been unsubscribed
                pass
        return REMOVE

    def __refuse(self, request, recip, comment, origmsg=None, lang=None):
        # As this message is going to the requestor, try to set the language
        # to his/her language choice, if they are a member.  Otherwise use the
        # list's preferred language.
        realname = self.real_name
        if lang is None:
            lang = self.getMemberLanguage(recip)
        text = Utils.maketext(
            'refuse.txt',
            {'listname' : realname,
             'request'  : request,
             'reason'   : comment,
             'adminaddr': self.GetOwnerEmail(),
            }, lang=lang, mlist=self)
        otrans = i18n.get_translation()
        i18n.set_language(lang)
        try:
            # add in original message, but not wrap/filled
            if origmsg:
                text = NL.join(
                    [text,
                     '---------- ' + _('Original Message') + ' ----------',
                     str(origmsg)
                     ])
            subject = _('Request to mailing list %(realname)s rejected')
        finally:
            i18n.set_translation(otrans)
        msg = Message.UserNotification(recip, self.GetOwnerEmail(),
                                       subject, text, lang)
        msg.send(self)

    def _UpdateRecords(self):
        # Subscription records have changed since MM2.0.x.  In that family,
        # the records were of length 4, containing the request time, the
        # address, the password, and the digest flag.  In MM2.1a2, they grew
        # an additional language parameter at the end.  In MM2.1a4, they grew
        # a fullname slot after the address.  This semi-public method is used
        # by the update script to coerce all subscription records to the
        # latest MM2.1 format.
        #
        # Held message records have historically either 5 or 6 items too.
        # These always include the requests time, the sender, subject, default
        # rejection reason, and message text.  When of length 6, it also
        # includes the message metadata dictionary on the end of the tuple.
        #
        # In Mailman 2.1.5 we converted these files to pickles.
        filename = os.path.join(self.fullpath(), 'request.db')
        try:
            fp = open(filename)
            try:
                self.__db = marshal.load(fp)
            finally:
                fp.close()
            os.unlink(filename)
        except IOError, e:
            if e.errno <> errno.ENOENT: raise
            filename = os.path.join(self.fullpath(), 'request.pck')
            try:
                fp = open(filename)
                try:
                    self.__db = cPickle.load(fp)
                finally:
                    fp.close()
            except IOError, e:
                if e.errno <> errno.ENOENT: raise
                self.__db = {}
        for id, x in self.__db.items():
            # A bug in versions 2.1.1 through 2.1.11 could have resulted in
            # just info being stored instead of (op, info)
            if len(x) == 2:
                op, info = x
            elif len(x) == 6:
                # This is the buggy info. Check for digest flag.
                if x[4] in (0, 1):
                    op = SUBSCRIPTION
                else:
                    op = HELDMSG
                self.__db[id] = op, x
                continue
            else:
                assert False, 'Unknown record format in %s' % self.__filename
            if op == SUBSCRIPTION:
                if len(info) == 4:
                    # pre-2.1a2 compatibility
                    when, addr, passwd, digest = info
                    fullname = ''
                    lang = self.preferred_language
                elif len(info) == 5:
                    # pre-2.1a4 compatibility
                    when, addr, passwd, digest, lang = info
                    fullname = ''
                else:
                    assert len(info) == 6, 'Unknown subscription record layout'
                    continue
                # Here's the new layout
                self.__db[id] = op, (when, addr, fullname, passwd,
                                     digest, lang)
            elif op == HELDMSG:
                if len(info) == 5:
                    when, sender, subject, reason, text = info
                    msgdata = {}
                else:
                    assert len(info) == 6, 'Unknown held msg record layout'
                    continue
                # Here's the new layout
                self.__db[id] = op, (when, sender, subject, reason,
                                     text, msgdata)
        # All done
        self.__closedb()



def readMessage(path):
    # For backwards compatibility, we must be able to read either a flat text
    # file or a pickle.
    ext = os.path.splitext(path)[1]
    fp = open(path)
    try:
        if ext == '.txt':
            msg = email.message_from_file(fp, Message.Message)
        else:
            assert ext == '.pck'
            msg = cPickle.load(fp)
    finally:
        fp.close()
    return msg