aboutsummaryrefslogtreecommitdiffstats
path: root/Mailman/BDBMemberAdaptor.py
blob: 589be626d59c3084df907dde75aa4f778dba8f47 (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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
# Copyright (C) 2003 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

"""A MemberAdaptor based on the Berkeley database wrapper for Python.

Requires Python 2.2.2 or newer, and PyBSDDB3 4.1.3 or newer.
"""

# To use, put the following in a file called extend.py in the mailing list's
# directory:
#
# from Mailman.BDBMemberAdaptor import extend
#
# that's it!

import os
import new
import time
import errno
import struct
import cPickle as pickle

try:
    # Python 2.3
    from bsddb import db
except ImportError:
    # earlier Pythons
    from bsddb3 import db

from Mailman import mm_cfg
from Mailman import Utils
from Mailman import Errors
from Mailman import MemberAdaptor
from Mailman.MailList import MailList
from Mailman.Logging.Syslog import syslog

STORAGE_VERSION = 'BA01'
FMT = '>BHB'
FMTSIZE = struct.calcsize(FMT)

REGDELIV = 1
DIGDELIV = 2
REGFLAG = struct.pack('>B', REGDELIV)
DIGFLAG = struct.pack('>B', DIGDELIV)

# Positional arguments for _unpack()
CPADDR = 0
PASSWD = 1
LANG = 2
NAME = 3
DIGEST = 4
OPTIONS = 5
STATUS = 6



class BDBMemberAdaptor(MemberAdaptor.MemberAdaptor):
    def __init__(self, mlist):
        self._mlist = mlist
        # metainfo -- {key -> value}
        #     This table contains storage metadata information.  The keys and
        #     values are simple strings of variable length.   Here are the
        #     valid keys:
        #
        #         version - the version of the database
        #
        #  members -- {address | rec}
        #     For all regular delivery members, this maps from the member's
        #     key to their data record, which is a string concatenated of the
        #     following:
        #
        #     -- fixed data (as a packed struct)
        #        + 1-byte digest or regular delivery flag
        #        + 2-byte option flags
        #        + 1-byte delivery status
        #     -- variable data (as a pickle of a tuple)
        #        + their case preserved address or ''
        #        + their plaintext password
        #        + their chosen language
        #        + their realname or ''
        #
        # status -- {address | status+time}
        #     Maps the member's key to their delivery status and change time.
        #     These are passed as a tuple and are pickled for storage.
        #
        # topics -- {address | topicstrings}
        #     Maps the member's key to their topic strings, concatenated and
        #     separated by SEP
        #
        # bounceinfo -- {address | bounceinfo}
        #     Maps the member's key to their bounceinfo, as a pickle
        #
        # Make sure the database directory exists
        path = os.path.join(mlist.fullpath(), 'member.db')
        exists = False
        try:
            os.mkdir(path, 02775)
        except OSError, e:
            if e.errno <> errno.EEXIST: raise
            exists = True
        # Create the environment
        self._env = env = db.DBEnv()
        if exists:
            # We must join an existing environment, otherwise we'll get
            # DB_RUNRECOVERY errors when the second process to open the
            # environment begins a transaction.  I don't get it.
            env.open(path, db.DB_JOINENV)
        else:
            env.open(path,
                     db.DB_CREATE |
                     db.DB_RECOVER |
                     db.DB_INIT_MPOOL |
                     db.DB_INIT_TXN
                     )
        self._txn = None
        self._tables = []
        self._metainfo = self._setupDB('metainfo')
        self._members = self._setupDB('members')
        self._status = self._setupDB('status')
        self._topics = self._setupDB('topics')
        self._bounceinfo = self._setupDB('bounceinfo')
        # Check the database version number
        version = self._metainfo.get('version')
        if version is None:
            # Initialize
            try:
                self.txn_begin()
                self._metainfo.put('version', STORAGE_VERSION, txn=self._txn)
            except:
                self.txn_abort()
                raise
            else:
                self.txn_commit()
        else:
            # Currently there's nothing to upgrade
            assert version == STORAGE_VERSION

    def _setupDB(self, name):
        d = db.DB(self._env)
        openflags = db.DB_CREATE
        # db 4.1 requires that databases be opened in a transaction.  We'll
        # use auto commit, but only if that flag exists (i.e. we're using at
        # least db 4.1).
        try:
            openflags |= db.DB_AUTO_COMMIT
        except AttributeError:
            pass
        d.open(name, db.DB_BTREE, openflags)
        self._tables.append(d)
        return d

    def _close(self):
        self.txn_abort()
        for d in self._tables:
            d.close()
        # Checkpoint the database twice, as recommended by Sleepycat
        self._checkpoint()
        self._checkpoint()
        self._env.close()

    def _checkpoint(self):
        self._env.txn_checkpoint(0, 0, db.DB_FORCE)

    def txn_begin(self):
        assert self._txn is None
        self._txn = self._env.txn_begin()

    def txn_commit(self):
        assert self._txn is not None
        self._txn.commit()
        self._checkpoint()
        self._txn = None

    def txn_abort(self):
        if self._txn is not None:
            self._txn.abort()
            self._checkpoint()
        self._txn = None

    def _unpack(self, member):
        # Assume member is a LCE (i.e. lowercase key)
        rec = self._members.get(member.lower())
        assert rec is not None
        fixed = struct.unpack(FMT, rec[:FMTSIZE])
        vari = pickle.loads(rec[FMTSIZE:])
        return vari + fixed

    def _pack(self, member, cpaddr, passwd, lang, name, digest, flags, status):
        # Assume member is a LCE (i.e. lowercase key)
        fixed = struct.pack(FMT, digest, flags, status)
        vari = pickle.dumps((cpaddr, passwd, lang, name))
        self._members.put(member.lower(), fixed+vari, txn=self._txn)

    # MemberAdaptor writeable interface

    def addNewMember(self, member, **kws):
        assert self._mlist.Locked()
        # Make sure this address isn't already a member
        if self.isMember(member):
            raise Errors.MMAlreadyAMember, member
        # Parse the keywords
        digest = False
        password = Utils.MakeRandomPassword()
        language = self._mlist.preferred_language
        realname = None
        if kws.has_key('digest'):
            digest = kws['digest']
            del kws['digest']
        if kws.has_key('password'):
            password = kws['password']
            del kws['password']
        if kws.has_key('language'):
            language = kws['language']
            del kws['language']
        if kws.has_key('realname'):
            realname = kws['realname']
            del kws['realname']
        # Assert that no other keywords are present
        if kws:
            raise ValueError, kws.keys()
        # Should we store the case-preserved address?
        if Utils.LCDomain(member) == member.lower():
            cpaddress = ''
        else:
            cpaddress = member
        # Calculate the realname
        if realname is None:
            realname = ''
        # Calculate the digest flag
        if digest:
            digest = DIGDELIV
        else:
            digest = REGDELIV
        self._pack(member.lower(),
                   cpaddress, password, language, realname,
                   digest, self._mlist.new_member_options,
                   MemberAdaptor.ENABLED)

    def removeMember(self, member):
        txn = self._txn
        assert txn is not None
        assert self._mlist.Locked()
        self.__assertIsMember(member)
        key = member.lower()
        # Remove the table entries
        self._members.delete(key, txn=txn)
        if self._status.has_key(key):
            self._status.delete(key, txn=txn)
        if self._topics.has_key(key):
            self._topics.delete(key, txn=txn)
        if self._bounceinfo.has_key(key):
            self._bounceinfo.delete(key, txn=txn)

    def changeMemberAddress(self, member, newaddress, nodelete=0):
        assert self._mlist.Locked()
        self.__assertIsMember(member)
        okey = member.lower()
        nkey = newaddress.lower()
        txn = self._txn
        assert txn is not None
        # First, store a new member record, changing the case preserved addr.
        # Then delete the old record.
        cpaddr, passwd, lang, name, digest, flags, sts = self._unpack(okey)
        self._pack(nkey, newaddress, passwd, lang, name, digest, flags, sts)
        if not nodelete:
            self._members.delete(okey, txn)
        # Copy over the status times, topics, and bounce info, if present
        timestr = self._status.get(okey)
        if timestr is not None:
            self._status.put(nkey, timestr, txn=txn)
            if not nodelete:
                self._status.delete(okey, txn)
        topics = self._topics.get(okey)
        if topics is not None:
            self._topics.put(nkey, topics, txn=txn)
            if not nodelete:
                self._topics.delete(okey, txn)
        binfo = self._bounceinfo.get(nkey)
        if binfo is not None:
            self._binfo.put(nkey, binfo, txn=txn)
            if not nodelete:
                self._binfo.delete(okey, txn)

    def setMemberPassword(self, member, password):
        assert self._mlist.Locked()
        self.__assertIsMember(member)
        member = member.lower()
        cpaddr, oldpw, lang, name, digest, flags, status = self._unpack(member)
        self._pack(member, cpaddr, password, lang, name, digest, flags, status)

    def setMemberLanguage(self, member, language):
        assert self._mlist.Locked()
        self.__assertIsMember(member)
        member = member.lower()
        cpaddr, passwd, olang, name, digest, flags, sts = self._unpack(member)
        self._pack(member, cpaddr, passwd, language, name, digest, flags, sts)

    def setMemberOption(self, member, flag, value):
        assert self._mlist.Locked()
        self.__assertIsMember(member)
        member = member.lower()
        cpaddr, passwd, lang, name, digest, options, sts = self._unpack(member)
        # Sanity check for the digest flag
        if flag == mm_cfg.Digests:
            if value:
                # Be sure the list supports digest delivery
                if not self._mlist.digestable:
                    raise Errors.CantDigestError
                digest = DIGDELIV
            else:
                # Be sure the list supports regular delivery
                if not self._mlist.nondigestable:
                    raise Errors.MustDigestError
                # When toggling off digest delivery, we want to be sure to set
                # things up so that the user receives one last digest,
                # otherwise they may lose some email
                self._mlist.one_last_digest[member] = cpaddr
                digest = REGDELIV
        else:
            if value:
                options |= flag
            else:
                options &= ~flag
        self._pack(member, cpaddr, passwd, lang, name, digest, options, sts)

    def setMemberName(self, member, realname):
        assert self._mlist.Locked()
        self.__assertIsMember(member)
        member = member.lower()
        cpaddr, passwd, lang, oldname, digest, flags, sts = self._unpack(
            member)
        self._pack(member, cpaddr, passwd, lang, realname, digest, flags, sts)

    def setMemberTopics(self, member, topics):
        assert self._mlist.Locked()
        self.__assertIsMember(member)
        member = member.lower()
        if topics:
            self._topics.put(member, SEP.join(topics), txn=self._txn)
        elif self._topics.has_key(member):
            # No record is the same as no topics
            self._topics.delete(member, self._txn)

    def setDeliveryStatus(self, member, status):
        assert status in (MemberAdaptor.ENABLED,  MemberAdaptor.UNKNOWN,
                          MemberAdaptor.BYUSER,   MemberAdaptor.BYADMIN,
                          MemberAdaptor.BYBOUNCE)
        assert self._mlist.Locked()
        self.__assertIsMember(member)
        if status == MemberAdaptor.ENABLED:
            # Enable by resetting their bounce info
            self.setBounceInfo(member, None)
        else:
            # Pickle up the status an the current time and store that in the
            # database.  Use binary mode.
            data = pickle.dumps((status, time.time()), 1)
            self._status.put(member.lower(), data, txn=self._txn)

    def setBounceInfo(self, member, info):
        assert self._mlist.Locked()
        self.__assertIsMember(member)
        member = member.lower()
        if info is None:
            # This means to reset the bounce and delivery status information
            if self._bounceinfo.has_key(member):
                self._bounceinfo.delete(member, self._txn)
            if self._status.has_key(member):
                self._status.delete(member, self._txn)
        else:
            # Use binary mode
            data = pickle.dumps(info, 1)
            self._status.put(member, data, txn=self._txn)

    # The readable interface

    # BAW: It would be more efficient to simply return the iterator, but
    # modules like admin.py can't handle that yet.  They requires lists.
    def getMembers(self):
        return list(_AllMembersIterator(self._members))

    def getRegularMemberKeys(self):
        return list(_DeliveryMemberIterator(self._members, REGFLAG))

    def getDigestMemberKeys(self):
        return list(_DeliveryMemberIterator(self._members, DIGFLAG))

    def __assertIsMember(self, member):
        if not self.isMember(member):
            raise Errors.NotAMemberError, member

    def isMember(self, member):
        return self._members.has_key(member.lower())

    def getMemberKey(self, member):
        self.__assertIsMember(member)
        return member.lower()

    def getMemberCPAddress(self, member):
        self.__assertIsMember(member)
        cpaddr = self._unpack(member)[CPADDR]
        if cpaddr:
            return cpaddr
        return member

    def getMemberCPAddresses(self, members):
        rtn = []
        for member in members:
            member = member.lower()
            if self._members.has_key(member):
                rtn.append(self._unpack(member)[CPADDR])
            else:
                rtn.append(None)
        return rtn

    def authenticateMember(self, member, response):
        self.__assertIsMember(member)
        passwd = self._unpack(member)[PASSWD]
        if passwd == response:
            return passwd
        return False

    def getMemberPassword(self, member):
        self.__assertIsMember(member)
        return self._unpack(member)[PASSWD]

    def getMemberLanguage(self, member):
        if not self.isMember(member):
            return self._mlist.preferred_language
        lang = self._unpack(member)[LANG]
        if lang in self._mlist.GetAvailableLanguages():
            return lang
        return self._mlist.preferred_language

    def getMemberOption(self, member, flag):
        self.__assertIsMember(member)
        if flag == mm_cfg.Digests:
            return self._unpack(member)[DIGEST] == DIGDELIV
        options = self._unpack(member)[OPTIONS]
        return bool(options & flag)

    def getMemberName(self, member):
        self.__assertIsMember(member)
        name = self._unpack(member)[NAME]
        return name or None

    def getMemberTopics(self, member):
        self.__assertIsMember(member)
        topics = self._topics.get(member.lower(), '')
        if not topics:
            return []
        return topics.split(SEP)

    def getDeliveryStatus(self, member):
        self.__assertIsMember(member)
        data = self._status.get(member.lower())
        if data is None:
            return MemberAdaptor.ENABLED
        status, when = pickle.loads(data)
        return status

    def getDeliveryStatusChangeTime(self, member):
        self.__assertIsMember(member)
        data = self._status.get(member.lower())
        if data is None:
            return 0
        status, when = pickle.loads(data)
        return when

    # BAW: see above, re iterators
    def getDeliveryStatusMembers(self, status=(MemberAdaptor.UNKNOWN,
                                               MemberAdaptor.BYUSER,
                                               MemberAdaptor.BYADMIN,
                                               MemberAdaptor.BYBOUNCE)):
        return list(_StatusMemberIterator(self._members, self._status, status))

    def getBouncingMembers(self):
        return list(_BouncingMembersIterator(self._bounceinfo))

    def getBounceInfo(self, member):
        self.__assertIsMember(member)
        return self._bounceinfo.get(member.lower())



class _MemberIterator:
    def __init__(self, table):
        self._table = table
        self._c = table.cursor()

    def __iter__(self):
        raise NotImplementedError

    def next(self):
        raise NotImplementedError

    def close(self):
        if self._c:
            self._c.close()
            self._c = None

    def __del__(self):
        self.close()


class _AllMembersIterator(_MemberIterator):
    def __iter__(self):
        return _AllMembersIterator(self._table)

    def next(self):
        rec = self._c.next()
        if rec:
            return rec[0]
        self.close()
        raise StopIteration


class _DeliveryMemberIterator(_MemberIterator):
    def __init__(self, table, flag):
        _MemberIterator.__init__(self, table)
        self._flag = flag

    def __iter__(self):
        return _DeliveryMemberIterator(self._table, self._flag)

    def next(self):
        rec = self._c.next()
        while rec:
            addr, data = rec
            if data[0] == self._flag:
                return addr
            rec = self._c.next()
        self.close()
        raise StopIteration


class _StatusMemberIterator(_MemberIterator):
    def __init__(self, table, statustab, status):
        _MemberIterator.__init__(self, table)
        self._statustab = statustab
        self._status = status

    def __iter__(self):
        return _StatusMemberIterator(self._table,
                                     self._statustab,
                                     self._status)

    def next(self):
        rec = self._c.next()
        while rec:
            addr = rec[0]
            data = self._statustab.get(addr)
            if data is None:
                status = MemberAdaptor.ENABLED
            else:
                status, when = pickle.loads(data)
            if status in self._status:
                return addr
            rec = self._c.next()
        self.close()
        raise StopIteration


class _BouncingMembersIterator(_MemberIterator):
    def __iter__(self):
        return _BouncingMembersIterator(self._table)

    def next(self):
        rec = self._c.next()
        if rec:
            return rec[0]
        self.close()
        raise StopIteration



# For extend.py
def fixlock(mlist):
    def Lock(self, timeout=0):
        MailList.Lock(self, timeout)
        try:
            self._memberadaptor.txn_begin()
        except:
            MailList.Unlock(self)
            raise
    mlist.Lock = new.instancemethod(Lock, mlist, MailList)


def fixsave(mlist):
    def Save(self):
        self._memberadaptor.txn_commit()
        MailList.Save(self)
    mlist.Save = new.instancemethod(Save, mlist, MailList)


def fixunlock(mlist):
    def Unlock(self):
        # It's fine to abort the transaction even if there isn't one in
        # process, say because the Save() already committed it
        self._memberadaptor.txn_abort()
        MailList.Unlock(self)
    mlist.Unlock = new.instancemethod(Unlock, mlist, MailList)


def extend(mlist):
    mlist._memberadaptor = BDBMemberAdaptor(mlist)
    fixlock(mlist)
    fixsave(mlist)
    fixunlock(mlist)
    # To make sure we got everything, let's actually delete the
    # OldStyleMemberships dictionaries.  Assume if it has one, it has all
    # attributes.
    try:
        del mlist.members
        del mlist.digest_members
        del mlist.passwords
        del mlist.language
        del mlist.user_options
        del mlist.usernames
        del mlist.topics_userinterest
        del mlist.delivery_status
        del mlist.bounce_info
    except AttributeError:
        pass
    # BAW: How can we ensure that the BDBMemberAdaptor is closed?