File: DB.py

package info (click to toggle)
pysrs 1.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 280 kB
  • sloc: python: 1,288; sh: 72; makefile: 23
file content (88 lines) | stat: -rw-r--r-- 2,418 bytes parent folder | download | duplicates (2)
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
# $Log$
# Revision 1.1.1.1  2005/06/03 04:13:18  customdesigned
# Initial import
#
# Revision 1.1.1.1  2004/03/19 05:23:13  stuart
# Import to CVS
#
#
# AUTHOR
# Shevek
# CPAN ID: SHEVEK
# cpan@anarres.org
# http://www.anarres.org/projects/
#
# Translated to Python by stuart@bmsi.com
# http://bmsi.com/python/milter.html
#
# Portions Copyright (c) 2004 Shevek. All rights reserved.
# Portions Copyright (c) 2004 Business Management Systems. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the same terms as Python itself.

try:
  import bsddb3 as bsddb
except:
  import bsddb
import time
import SRS
from .Base import Base
from pickle import dumps, loads

class DB(Base):
  """A MLDBM based Sender Rewriting Scheme

SYNOPSIS

        from SRS.DB import DB
        srs = DB(Database='/var/run/srs.db', ...)

DESCRIPTION

See Base.py for details of the standard SRS subclass interface.
This module provides the methods compile() and parse().

This module requires one extra parameter to the constructor, a filename
for a Berkeley DB_File database.

BUGS

This code relies on not getting collisions in the cryptographic
hash. This can and should be fixed.

The database is not garbage collected."""

  def __init__(self,database='/var/run/srs.db',hashlength=24,*args,**kw):
    Base.__init__(self,hashlength=hashlength,*args,**kw)
    assert database, "No database specified for SRS.DB"
    self.dbm = bsddb.btopen(database,'c')

  def compile(self,sendhost,senduser,srshost=None):
    ts = time.time()

    data = dumps((ts,sendhost,senduser))

    # We rely on not getting collisions in this hash.
    hash = self.hash_create(sendhost.encode(),senduser.encode())

    self.dbm[hash] = data

    # Note that there are 4 fields here and that sendhost may
    # not contain a + sign. Therefore, we do not need to escape
    # + signs anywhere in order to reverse this transformation.
    return SRS.SRS0TAG + self.separator + hash.decode()

  def parse(self,user,srshost=None):
    user,m = self.srs0re.subn('',user,1)
    assert m, "Reverse address does not match %s." % self.srs0re.pattern

    hash = user
    data = self.dbm[hash.encode()]
    ts,sendhost,senduser = loads(data)

    assert self.hash_verify(hash.encode(),sendhost.encode(),senduser.encode()), "Invalid hash"

    assert self.time_check(ts), "Invalid timestamp"

    return (sendhost, senduser)