~ar-python-hackers/authentication-results-python/trunk

« back to all changes in this revision

Viewing changes to authres/core.py

  • Committer: Julian Mehnle
  • Date: 2012-01-23 02:32:27 UTC
  • Revision ID: jmehnle@agari.com-20120123023227-37nqxoxwvnt7zgp8
Convert authres module into a Python package, moving it into an authres/ subdirectory and splitting it into __{init,main}__.py.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# coding: utf-8
2
 
 
3
 
# Copyright © 2011-2013 Julian Mehnle <julian@mehnle.net>,
4
 
# Copyright © 2011-2013 Scott Kitterman <scott@kitterman.com>
5
 
#
6
 
# Licensed under the Apache License, Version 2.0 (the "License");
7
 
# you may not use this file except in compliance with the License.
8
 
# You may obtain a copy of the License at
9
 
#
10
 
#  http://www.apache.org/licenses/LICENSE-2.0
11
 
#
12
 
# Unless required by applicable law or agreed to in writing, software
13
 
# distributed under the License is distributed on an "AS IS" BASIS,
14
 
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 
# See the License for the specific language governing permissions and
16
 
# limitations under the License.
17
 
 
18
 
"""
19
 
Module for parsing ``Authentication-Results`` headers as defined in RFC 5451,
20
 
7001, and 7601.
21
 
"""
22
 
 
23
 
#MODULE = 'authres'
24
 
 
25
 
__author__  = 'Julian Mehnle, Scott Kitterman'
26
 
__email__   = 'julian@mehnle.net'
27
 
 
28
 
import re
29
 
 
30
 
# Helper functions
31
 
###############################################################################
32
 
 
33
 
retype = type(re.compile(''))
34
 
 
35
 
def isre(obj):
36
 
    return isinstance(obj, retype)
37
 
 
38
 
# Patterns
39
 
###############################################################################
40
 
 
41
 
RFC2045_TOKEN_PATTERN       = r"[A-Za-z0-9!#$%&'*+.^_`{|}~-]+"    # Printable ASCII w/o tspecials
42
 
RFC5234_WSP_PATTERN         = r'[\t ]'
43
 
RFC5234_VCHAR_PATTERN       = r'[\x21-\x7e]'                      # Printable ASCII
44
 
RFC5322_QUOTED_PAIR_PATTERN = r'\\[\t \x21-\x7e]'
45
 
RFC5322_FWS_PATTERN         = r'(?:%s*(?:\r\n|\n))?%s+' % (RFC5234_WSP_PATTERN, RFC5234_WSP_PATTERN)
46
 
RFC5322_CTEXT_PATTERN       = r'[\x21-\x27\x2a-\x5b\x5d-\x7e]'    # Printable ASCII w/o ()\
47
 
RFC5322_ATEXT_PATTERN       = r"[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]"   # Printable ASCII w/o specials
48
 
RFC5322_QTEXT_PATTERN       = r'[\x21\x23-\x5b\x5d-\x7e]'         # Printable ASCII w/o "\
49
 
KTEXT_PATTERN               = r"[A-Za-z0-9!#$%&'*+?^_`{|}~-]"     # Like atext, w/o /=
50
 
PTEXT_PATTERN               = r"[A-Za-z0-9!#$%&'*+/=?^_`{|}~.@-]"
51
 
 
52
 
# Exceptions
53
 
###############################################################################
54
 
 
55
 
class AuthResError(Exception):
56
 
    "Generic exception generated by the `authres` package"
57
 
 
58
 
    def __init__(self, message = None):
59
 
        Exception.__init__(self, message)
60
 
        self.message = message
61
 
 
62
 
class SyntaxError(AuthResError):
63
 
    "Syntax error while parsing ``Authentication-Results`` header"
64
 
 
65
 
    def __init__(self, message = None, parse_text = None):
66
 
        AuthResError.__init__(self, message)
67
 
        if parse_text is None or len(parse_text) <= 40:
68
 
            self.parse_text = parse_text
69
 
        else:
70
 
            self.parse_text = parse_text[0:40] + '...'
71
 
 
72
 
    def __str__(self):
73
 
        if self.message and self.parse_text:
74
 
            return 'Syntax error: {0} at: {1}'.format(self.message, self.parse_text)
75
 
        elif self.message:
76
 
            return 'Syntax error: {0}'.format(self.message)
77
 
        elif self.parse_text:
78
 
            return 'Syntax error at: {0}'.format(self.parse_text)
79
 
        else:
80
 
            return 'Syntax error'
81
 
 
82
 
class UnsupportedVersionError(AuthResError):
83
 
    "Unsupported ``Authentication-Results`` header version"
84
 
 
85
 
    def __init__(self, message = None, version = None):
86
 
        message = message or \
87
 
            'Unsupported Authentication-Results header version: %s' % version
88
 
        AuthResError.__init__(self, message)
89
 
        self.version = version
90
 
 
91
 
class OrphanCommentError(AuthResError):
92
 
    "Comment without associated header element"
93
 
 
94
 
# Main classes
95
 
###############################################################################
96
 
 
97
 
# QuotableValue class
98
 
# =============================================================================
99
 
 
100
 
class QuotableValue(str):
101
 
    """
102
 
    An RFC 5451 ``value``/``pvalue`` with the capability to quote itself as an
103
 
    RFC 5322 ``quoted-string`` if necessary.
104
 
    """
105
 
    def quote_if_needed(self):
106
 
        if re.search(r'@', self):
107
 
            return self
108
 
        elif re.match(r'^%s$' % RFC2045_TOKEN_PATTERN, self):
109
 
            return self
110
 
        else:
111
 
            return '"%s"' % re.sub(r'(["\\])', r'\\\1', self)  # Escape "\
112
 
 
113
 
# AuthenticationResultProperty class
114
 
# =============================================================================
115
 
 
116
 
class AuthenticationResultProperty(object):
117
 
    """
118
 
    A property (``type.name=value``) of a result clause of an
119
 
    ``Authentication-Results`` header
120
 
    """
121
 
 
122
 
    def __init__(self, type, name, value = None, comment = None):
123
 
        self.type    = type.lower()
124
 
        self.name    = name.lower()
125
 
        self.value   = value and QuotableValue(value)
126
 
        self.comment = comment
127
 
 
128
 
    def __str__(self):
129
 
        if self.comment:
130
 
            return '%s.%s=%s (%s)' % (self.type, self.name, self.value.quote_if_needed(), self.comment)
131
 
        else:
132
 
            return '%s.%s=%s' % (self.type, self.name, self.value.quote_if_needed())
133
 
 
134
 
# Clarification of identifier naming:
135
 
# The following function acts as a factory for Python property attributes to
136
 
# be bound to a class, so it is named `make_result_class_properties`.  Its
137
 
# nested `getter` and `setter` functions use the identifier `result_property`
138
 
# to refer to an instance of the `AuthenticationResultProperty` class.
139
 
def make_result_class_properties(type, name):
140
 
    """
141
 
    Return a property attribute to be bound to an `AuthenticationResult` class
142
 
    for accessing the `AuthenticationResultProperty` objects in its `properties`
143
 
    attribute.
144
 
    """
145
 
 
146
 
    def value_getter(self, type = type, name = name):
147
 
        result_property = self._find_first_property(type, name)
148
 
        return result_property and result_property.value
149
 
 
150
 
    def comment_getter(self, type = type, name = name):
151
 
        result_property = self._find_first_property(type, name)
152
 
        return result_property and result_property.comment
153
 
 
154
 
    def value_setter(self, value, type = type, name = name):
155
 
        result_property = self._find_first_property(type, name)
156
 
        if not result_property:
157
 
            result_property = AuthenticationResultProperty(type, name)
158
 
            self.properties.append(result_property)
159
 
        result_property.value = value and QuotableValue(value)
160
 
 
161
 
    def comment_setter(self, comment, type = type, name = name):
162
 
        result_property = self._find_first_property(type, name)
163
 
        if not result_property:
164
 
            raise OrphanCommentError(
165
 
                "Cannot include result property comment without associated result property: %s.%s" % (type, name))
166
 
        result_property.comment = comment
167
 
 
168
 
    return property(value_getter, value_setter), property(comment_getter, comment_setter)
169
 
 
170
 
# AuthenticationResult and related classes
171
 
# =============================================================================
172
 
 
173
 
class BaseAuthenticationResult(object): pass
174
 
 
175
 
class NoneAuthenticationResult(BaseAuthenticationResult):
176
 
    "Sole ``none`` clause of an empty ``Authentication-Results`` header"
177
 
 
178
 
    def __init__(self, comment = None):
179
 
        self.comment = comment
180
 
 
181
 
    def __str__(self):
182
 
        if self.comment:
183
 
            return 'none (%s)' % self.comment
184
 
        else:
185
 
            return 'none'
186
 
 
187
 
class AuthenticationResult(BaseAuthenticationResult):
188
 
    "Generic result clause of an ``Authentication-Results`` header"
189
 
 
190
 
    def __init__(self, method, version = None,
191
 
        result               = None,  result_comment               = None,
192
 
        reason               = None,  reason_comment               = None,
193
 
        properties = None
194
 
    ):
195
 
        self.method         = method.lower()
196
 
        self.version        = version and version.lower()
197
 
        self.result         = result.lower()
198
 
        if not self.result:
199
 
            raise ValueError('Required result argument missing or None or empty')
200
 
        self.result_comment = result_comment
201
 
        self.reason         = reason and QuotableValue(re.sub(r'[^\x20-\x7e]', '?', reason))
202
 
            # Remove unprintable characters
203
 
        self.reason_comment = reason_comment
204
 
        self.properties     = properties or []
205
 
 
206
 
    def __str__(self):
207
 
        strs = []
208
 
        strs.append(self.method)
209
 
        if self.version:
210
 
            strs.append('/')
211
 
            strs.append(self.version)
212
 
        strs.append('=')
213
 
        strs.append(self.result)
214
 
        if self.result_comment:
215
 
            strs.append(' (%s)' % self.result_comment)
216
 
        if self.reason:
217
 
            strs.append(' reason=%s' % self.reason.quote_if_needed())
218
 
            if self.reason_comment:
219
 
                strs.append(' (%s)' % self.reason_comment)
220
 
        for property_ in self.properties:
221
 
            strs.append(' ')
222
 
            strs.append(str(property_))
223
 
        return ''.join(strs)
224
 
 
225
 
    def _find_first_property(self, type, name):
226
 
        properties = [
227
 
            property
228
 
            for property
229
 
            in self.properties
230
 
            if property.type == type and property.name == name
231
 
        ]
232
 
        return properties[0] if properties else None
233
 
 
234
 
class DKIMAuthenticationResult(AuthenticationResult):
235
 
    "DKIM result clause of an ``Authentication-Results`` header"
236
 
 
237
 
    METHOD = 'dkim'
238
 
 
239
 
    def __init__(self, version = None,
240
 
        result               = None,  result_comment               = None,
241
 
        reason               = None,  reason_comment               = None,
242
 
        properties = None,
243
 
        header_d             = None,  header_d_comment             = None,
244
 
        header_i             = None,  header_i_comment             = None
245
 
    ):
246
 
        AuthenticationResult.__init__(self, self.METHOD, version,
247
 
            result, result_comment, reason, reason_comment, properties)
248
 
        if header_d:                     self.header_d                     = header_d
249
 
        if header_d_comment:             self.header_d_comment             = header_d_comment
250
 
        if header_i:                     self.header_i                     = header_i
251
 
        if header_i_comment:             self.header_i_comment             = header_i_comment
252
 
 
253
 
    header_d,             header_d_comment             = make_result_class_properties('header', 'd')
254
 
    header_i,             header_i_comment             = make_result_class_properties('header', 'i')
255
 
 
256
 
    def match_signature(self, signature_d):
257
 
        """Match authentication result against a DKIM signature by ``header.d``."""
258
 
 
259
 
        return self.header_d == signature_d
260
 
 
261
 
class DomainKeysAuthenticationResult(AuthenticationResult):
262
 
    "DomainKeys result clause of an ``Authentication-Results`` header"
263
 
 
264
 
    METHOD = 'domainkeys'
265
 
 
266
 
    def __init__(self, version = None,
267
 
        result               = None,  result_comment               = None,
268
 
        reason               = None,  reason_comment               = None,
269
 
        properties = None,
270
 
        header_d             = None,  header_d_comment             = None,
271
 
        header_from          = None,  header_from_comment          = None,
272
 
        header_sender        = None,  header_sender_comment        = None
273
 
    ):
274
 
        AuthenticationResult.__init__(self, self.METHOD, version,
275
 
            result, result_comment, reason, reason_comment, properties)
276
 
        if header_d:                     self.header_d                     = header_d
277
 
        if header_d_comment:             self.header_d_comment             = header_d_comment
278
 
        if header_from:                  self.header_from                  = header_from
279
 
        if header_from_comment:          self.header_from_comment          = header_from_comment
280
 
        if header_sender:                self.header_sender                = header_sender
281
 
        if header_sender_comment:        self.header_sender_comment        = header_sender_comment
282
 
 
283
 
    header_d,             header_d_comment             = make_result_class_properties('header', 'd')
284
 
    header_from,          header_from_comment          = make_result_class_properties('header', 'from')
285
 
    header_sender,        header_sender_comment        = make_result_class_properties('header', 'sender')
286
 
 
287
 
    def match_signature(self, signature_d):
288
 
        """Match authentication result against a DomainKeys signature by ``header.d``."""
289
 
 
290
 
        return self.header_d == signature_d
291
 
 
292
 
class SPFAuthenticationResult(AuthenticationResult):
293
 
    "SPF result clause of an ``Authentication-Results`` header"
294
 
 
295
 
    METHOD = 'spf'
296
 
 
297
 
    def __init__(self, version = None,
298
 
        result               = None,  result_comment               = None,
299
 
        reason               = None,  reason_comment               = None,
300
 
        properties = None,
301
 
        smtp_helo            = None,  smtp_helo_comment            = None,
302
 
        smtp_mailfrom        = None,  smtp_mailfrom_comment        = None
303
 
    ):
304
 
        AuthenticationResult.__init__(self, self.METHOD, version,
305
 
            result, result_comment, reason, reason_comment, properties)
306
 
        if smtp_helo:                    self.smtp_helo                    = smtp_helo
307
 
        if smtp_helo_comment:            self.smtp_helo_comment            = smtp_helo_comment
308
 
        if smtp_mailfrom:                self.smtp_mailfrom                = smtp_mailfrom
309
 
        if smtp_mailfrom_comment:        self.smtp_mailfrom_comment        = smtp_mailfrom_comment
310
 
 
311
 
    smtp_helo,            smtp_helo_comment            = make_result_class_properties('smtp', 'helo')
312
 
    smtp_mailfrom,        smtp_mailfrom_comment        = make_result_class_properties('smtp', 'mailfrom')
313
 
 
314
 
class SenderIDAuthenticationResult(AuthenticationResult):
315
 
    "Sender ID result clause of an ``Authentication-Results`` header"
316
 
 
317
 
    METHOD = 'sender-id'
318
 
 
319
 
    def __init__(self, version = None,
320
 
        result               = None,  result_comment               = None,
321
 
        reason               = None,  reason_comment               = None,
322
 
        properties = None,
323
 
        header_from          = None,  header_from_comment          = None,
324
 
        header_sender        = None,  header_sender_comment        = None,
325
 
        header_resent_from   = None,  header_resent_from_comment   = None,
326
 
        header_resent_sender = None,  header_resent_sender_comment = None
327
 
    ):
328
 
        AuthenticationResult.__init__(self, self.METHOD, version,
329
 
            result, result_comment, reason, reason_comment, properties)
330
 
        if header_from:                  self.header_from                  = header_from
331
 
        if header_from_comment:          self.header_from_comment          = header_from_comment
332
 
        if header_sender:                self.header_sender                = header_sender
333
 
        if header_sender_comment:        self.header_sender_comment        = header_sender_comment
334
 
        if header_resent_from:           self.header_resent_from           = header_resent_from
335
 
        if header_resent_from_comment:   self.header_resent_from_comment   = header_resent_from_comment
336
 
        if header_resent_sender:         self.header_resent_sender         = header_resent_sender
337
 
        if header_resent_sender_comment: self.header_resent_sender_comment = header_resent_sender_comment
338
 
 
339
 
    header_from,          header_from_comment          = make_result_class_properties('header', 'from')
340
 
    header_sender,        header_sender_comment        = make_result_class_properties('header', 'sender')
341
 
    header_resent_from,   header_resent_from_comment   = make_result_class_properties('header', 'resent-from')
342
 
    header_resent_sender, header_resent_sender_comment = make_result_class_properties('header', 'resent-sender')
343
 
 
344
 
    @property
345
 
    def header_pra(self):
346
 
        return (
347
 
            self.header_resent_sender or
348
 
            self.header_resent_from   or
349
 
            self.header_sender        or
350
 
            self.header_from
351
 
        )
352
 
 
353
 
    @property
354
 
    def header_pra_comment(self):
355
 
        if   self.header_resent_sender:
356
 
            return self.header_resent_sender_comment
357
 
        elif self.header_resent_from:
358
 
            return self.header_resent_from_comment
359
 
        elif self.header_sender:
360
 
            return self.header_sender_comment
361
 
        elif self.header_from:
362
 
            return self.header_from_comment
363
 
        else:
364
 
            return None
365
 
 
366
 
class IPRevAuthenticationResult(AuthenticationResult):
367
 
    "iprev result clause of an ``Authentication-Results`` header"
368
 
 
369
 
    METHOD = 'iprev'
370
 
 
371
 
    def __init__(self, version = None,
372
 
        result               = None,  result_comment               = None,
373
 
        reason               = None,  reason_comment               = None,
374
 
        properties = None,
375
 
        policy_iprev         = None,  policy_iprev_comment         = None
376
 
    ):
377
 
        AuthenticationResult.__init__(self, self.METHOD, version,
378
 
            result, result_comment, reason, reason_comment, properties)
379
 
        if policy_iprev:                 self.policy_iprev                 = policy_iprev
380
 
        if policy_iprev_comment:         self.policy_iprev_comment         = policy_iprev_comment
381
 
 
382
 
    policy_iprev,         policy_iprev_comment         = make_result_class_properties('policy', 'iprev')
383
 
 
384
 
class SMTPAUTHAuthenticationResult(AuthenticationResult):
385
 
    "SMTP AUTH result clause of an ``Authentication-Results`` header"
386
 
 
387
 
    METHOD = 'auth'
388
 
 
389
 
    def __init__(self, version = None,
390
 
        result               = None,  result_comment               = None,
391
 
        reason               = None,  reason_comment               = None,
392
 
        properties = None,
393
 
        # Added in RFC 7601, SMTP Auth method can refer to either the identity
394
 
        # confirmed in the auth command or the identity in auth parameter of
395
 
        # the SMTP Mail command, so we cover either option.
396
 
        smtp_auth            = None,  smtp_auth_comment            = None,
397
 
        smtp_mailfrom        = None,  smtp_mailfrom_comment        = None,
398
 
    ):
399
 
        AuthenticationResult.__init__(self, self.METHOD, version,
400
 
            result, result_comment, reason, reason_comment, properties)
401
 
        if smtp_auth:                    self.smtp_auth                    = smtp_auth
402
 
        if smtp_auth_comment:            self.smtp_auth_comment            = smtp_auth_comment
403
 
        if smtp_mailfrom:                self.smtp_mailfrom                = smtp_mailfrom
404
 
        if smtp_mailfrom_comment:        self.smtp_mailfrom_comment        = smtp_mailfrom_comment
405
 
 
406
 
    smtp_mailfrom,        smtp_mailfrom_comment        = make_result_class_properties('smtp', 'mailfrom')
407
 
    smtp_auth,            smtp_auth_comment            = make_result_class_properties('smtp', 'auth')
408
 
 
409
 
# AuthenticationResultsHeader class
410
 
# =============================================================================
411
 
 
412
 
class AuthenticationResultsHeader(object):
413
 
    VERSIONS = ['1']
414
 
 
415
 
    NONE_RESULT = NoneAuthenticationResult()
416
 
 
417
 
    HEADER_FIELD_NAME = 'Authentication-Results'
418
 
    HEADER_FIELD_PATTERN = re.compile(r'^Authentication-Results:\s*', re.I)
419
 
 
420
 
    @classmethod
421
 
    def parse(self, feature_context, string):
422
 
        """
423
 
        Creates an `AuthenticationResultsHeader` object by parsing an ``Authentication-
424
 
        Results`` header (expecting the field name at the beginning).  Expects the
425
 
        header to have been unfolded.
426
 
        """
427
 
        string, n = self.HEADER_FIELD_PATTERN.subn('', string, 1)
428
 
        if n == 1:
429
 
            return self.parse_value(feature_context, string)
430
 
        else:
431
 
            raise SyntaxError('parse_with_name', 'Not an "Authentication-Results" header field: {0}'.format(string))
432
 
 
433
 
    @classmethod
434
 
    def parse_value(self, feature_context, string):
435
 
        """
436
 
        Creates an `AuthenticationResultsHeader` object by parsing an ``Authentication-
437
 
        Results`` header value.  Expects the header value to have been unfolded.
438
 
        """
439
 
        header = self(feature_context)
440
 
        header._parse_text = string.rstrip('\r\n\t ')
441
 
        header._parse()
442
 
        return header
443
 
 
444
 
    def __init__(self,
445
 
        feature_context,
446
 
        authserv_id = None,  authserv_id_comment = None,
447
 
        version     = None,  version_comment     = None,
448
 
        results     = None
449
 
    ):
450
 
        self.feature_context     = feature_context
451
 
        self.authserv_id         = authserv_id and authserv_id.lower()
452
 
        self.authserv_id_comment = authserv_id_comment
453
 
        self.version             = version     and str(version).lower()
454
 
        if self.version and not self.version in self.VERSIONS:
455
 
            raise UnsupportedVersionError(version = self.version)
456
 
        self.version_comment     = version_comment
457
 
        if self.version_comment and not self.version:
458
 
            raise OrphanCommentError('Cannot include header version comment without associated header version')
459
 
        self.results             = results or []
460
 
 
461
 
    def __str__(self):
462
 
        return ''.join((self.HEADER_FIELD_NAME, ': ', self.header_value()))
463
 
 
464
 
    def header_value(self):
465
 
        "Return just the value of Authentication-Results header."
466
 
        strs = []
467
 
        strs.append(self.authserv_id)
468
 
        if self.authserv_id_comment:
469
 
            strs.append(' (%s)' % self.authserv_id_comment)
470
 
        if self.version:
471
 
            strs.append(' ')
472
 
            strs.append(self.version)
473
 
            if self.version_comment:
474
 
                strs.append(' (%s)' % self.version_comment)
475
 
        if len(self.results):
476
 
            for result in self.results:
477
 
                strs.append('; ')
478
 
                strs.append(str(result))
479
 
        else:
480
 
            strs.append('; ')
481
 
            strs.append(str(self.NONE_RESULT))
482
 
        return ''.join(strs)
483
 
 
484
 
    # Principal parser methods
485
 
    # =========================================================================
486
 
 
487
 
    def _parse(self):
488
 
        authserv_id = self._parse_authserv_id()
489
 
        if not authserv_id:
490
 
            raise SyntaxError('Expected authserv-id', self._parse_text)
491
 
 
492
 
        self._parse_rfc5322_cfws()
493
 
 
494
 
        version = self._parse_version()
495
 
        if version and not version in self.VERSIONS:
496
 
            raise UnsupportedVersionError(version = version)
497
 
 
498
 
        self._parse_rfc5322_cfws()
499
 
 
500
 
        results = []
501
 
        result = True
502
 
        while result:
503
 
            result = self._parse_resinfo()
504
 
            if result:
505
 
                results.append(result)
506
 
                if result == self.NONE_RESULT:
507
 
                    break
508
 
        if not len(results):
509
 
            raise SyntaxError('Expected "none" or at least one resinfo', self._parse_text)
510
 
        elif results == [self.NONE_RESULT]:
511
 
            results = []
512
 
 
513
 
        self._parse_rfc5322_cfws()
514
 
        self._parse_end()
515
 
 
516
 
        self.authserv_id = authserv_id.lower()
517
 
        self.version     = version and version.lower()
518
 
        self.results     = results
519
 
 
520
 
    def _parse_authserv_id(self):
521
 
        return self._parse_rfc5322_dot_atom()
522
 
 
523
 
    def _parse_version(self):
524
 
        version_match = self._parse_pattern(r'\d+')
525
 
        self._parse_rfc5322_cfws()
526
 
        return version_match and version_match.group()
527
 
 
528
 
    def _parse_resinfo(self):
529
 
        self._parse_rfc5322_cfws()
530
 
        if not self._parse_pattern(r';'):
531
 
            return
532
 
        self._parse_rfc5322_cfws()
533
 
        if self._parse_pattern(r'none'):
534
 
            return self.NONE_RESULT
535
 
        else:
536
 
            method, version, result = self._parse_methodspec()
537
 
            self._parse_rfc5322_cfws()
538
 
            reason = self._parse_reasonspec()
539
 
            properties = []
540
 
            property_ = True
541
 
            while property_:
542
 
                self._parse_rfc5322_cfws()
543
 
                property_ = self._parse_propspec()
544
 
                if property_:
545
 
                    properties.append(property_)
546
 
            return self.feature_context.result(method, version, result, None, reason, None, properties)
547
 
 
548
 
    def _parse_methodspec(self):
549
 
        self._parse_rfc5322_cfws()
550
 
        method, version = self._parse_method()
551
 
        self._parse_rfc5322_cfws()
552
 
        if not self._parse_pattern(r'='):
553
 
            raise SyntaxError('Expected "="', self._parse_text)
554
 
        self._parse_rfc5322_cfws()
555
 
        result = self._parse_rfc5322_dot_atom()
556
 
        if not result:
557
 
            raise SyntaxError('Expected result', self._parse_text)
558
 
        return (method, version, result)
559
 
 
560
 
    def _parse_method(self):
561
 
        method = self._parse_dot_key_atom()
562
 
        if not method:
563
 
            raise SyntaxError('Expected method', self._parse_text)
564
 
        self._parse_rfc5322_cfws()
565
 
        if not self._parse_pattern(r'/'):
566
 
            return (method, None)
567
 
        self._parse_rfc5322_cfws()
568
 
        version_match = self._parse_pattern(r'\d+')
569
 
        if not version_match:
570
 
            raise SyntaxError('Expected version', self._parse_text)
571
 
        return (method, version_match.group())
572
 
 
573
 
    def _parse_reasonspec(self):
574
 
        if self._parse_pattern(r'reason'):
575
 
            self._parse_rfc5322_cfws()
576
 
            if not self._parse_pattern(r'='):
577
 
                raise SyntaxError('Expected "="', self._parse_text)
578
 
            self._parse_rfc5322_cfws()
579
 
            reasonspec = self._parse_rfc2045_value()
580
 
            if not reasonspec:
581
 
                raise SyntaxError('Expected reason', self._parse_text)
582
 
            return reasonspec
583
 
 
584
 
    def _parse_propspec(self):
585
 
        ptype = self._parse_key_atom()
586
 
        if not ptype:
587
 
            return
588
 
        elif ptype.lower() not in ['smtp', 'header', 'body', 'policy']:
589
 
            raise SyntaxError('Invalid ptype; expected any of "smtp", "header", "body", "policy", got "%s"' % ptype, self._parse_text)
590
 
        self._parse_rfc5322_cfws()
591
 
        if not self._parse_pattern(r'\.'):
592
 
            raise SyntaxError('Expected "."', self._parse_text)
593
 
        self._parse_rfc5322_cfws()
594
 
        property_ = self._parse_dot_key_atom()
595
 
        self._parse_rfc5322_cfws()
596
 
        if not self._parse_pattern(r'='):
597
 
            raise SyntaxError('Expected "="', self._parse_text)
598
 
        pvalue = self._parse_pvalue()
599
 
        if pvalue is None:
600
 
            raise SyntaxError('Expected pvalue', self._parse_text)
601
 
        return AuthenticationResultProperty(ptype, property_, pvalue)
602
 
 
603
 
    def _parse_pvalue(self):
604
 
        self._parse_rfc5322_cfws()
605
 
 
606
 
        # The original rule is (modulo CFWS):
607
 
        #
608
 
        #     pvalue = [ [local-part] "@" ] domain-name / value
609
 
        #     value  = token / quoted-string
610
 
        #
611
 
        # Distinguishing <token> from <domain-name> may require backtracking,
612
 
        # and in order to avoid the need for that, the following is a simpli-
613
 
        # fication of the <pvalue> rule from RFC 5451, erring on the side of
614
 
        # laxity.
615
 
        #
616
 
        # Since <local-part> is either a <quoted-string> or <dot-atom>, and
617
 
        # <value> is either a <quoted-string> or a <token>, and <dot-atom> and
618
 
        # <token> are very similar (<dot-atom> is a superset of <token> except
619
 
        # that multiple dots may not be adjacent), we allow a union of ".",
620
 
        # "@" and <atext> characters (jointly denoted <ptext>) in the place of
621
 
        # <dot-atom> and <token>.
622
 
        #
623
 
        # Furthermore we allow an empty string by requiring a sequence of zero
624
 
        # or more, rather than one or more (as required by RFC 2045's <token>),
625
 
        # <ptext> characters.
626
 
        #
627
 
        # We then allow four patterns:
628
 
        #
629
 
        #     pvalue = quoted-string                 /
630
 
        #              quoted-string "@" domain-name /
631
 
        #                            "@" domain-name /
632
 
        #              *ptext
633
 
 
634
 
        quoted_string = self._parse_rfc5322_quoted_string()
635
 
        if quoted_string:
636
 
            if self._parse_pattern(r'@'):
637
 
                # quoted-string "@" domain-name
638
 
                domain_name = self._parse_rfc5322_dot_atom()
639
 
                self._parse_rfc5322_cfws()
640
 
                if domain_name:
641
 
                    return '"%s"@%s' % (quoted_string, domain_name)
642
 
            else:
643
 
                # quoted-string
644
 
                self._parse_rfc5322_cfws()
645
 
                # Look ahead to see whether pvalue terminates after quoted-string as expected:
646
 
                if re.match(r';|$', self._parse_text):
647
 
                    return quoted_string
648
 
        else:
649
 
            if self._parse_pattern(r'@'):
650
 
                # "@" domain-name
651
 
                domain_name = self._parse_rfc5322_dot_atom()
652
 
                self._parse_rfc5322_cfws()
653
 
                if domain_name:
654
 
                    return '@' + domain_name
655
 
            else:
656
 
                # *ptext
657
 
                pvalue_match = self._parse_pattern(r'%s*' % PTEXT_PATTERN)
658
 
                self._parse_rfc5322_cfws()
659
 
                if pvalue_match:
660
 
                    return pvalue_match.group()
661
 
 
662
 
    def _parse_end(self):
663
 
        if self._parse_text == '':
664
 
            return True
665
 
        else:
666
 
            raise SyntaxError('Expected end of text', self._parse_text)
667
 
 
668
 
    # Generic grammar parser methods
669
 
    # =========================================================================
670
 
 
671
 
    def _parse_pattern(self, pattern):
672
 
        match = [None]
673
 
 
674
 
        def matched(m):
675
 
            match[0] = m
676
 
            return ''
677
 
 
678
 
        # TODO: This effectively recompiles most patterns on each use, which
679
 
        #       is far from efficient.  This should be rearchitected.
680
 
        regexp = pattern if isre(pattern) else re.compile(r'^' + pattern, re.I)
681
 
        self._parse_text = regexp.sub(matched, self._parse_text, 1)
682
 
        return match[0]
683
 
 
684
 
    def _parse_rfc2045_value(self):
685
 
        return self._parse_rfc2045_token() or self._parse_rfc5322_quoted_string()
686
 
 
687
 
    def _parse_rfc2045_token(self):
688
 
        token_match = self._parse_pattern(RFC2045_TOKEN_PATTERN)
689
 
        return token_match and token_match.group()
690
 
 
691
 
    def _parse_rfc5322_quoted_string(self):
692
 
        self._parse_rfc5322_cfws()
693
 
        if not self._parse_pattern(r'^"'):
694
 
            return
695
 
        all_qcontent = ''
696
 
        qcontent = True
697
 
        while qcontent:
698
 
            fws_match = self._parse_pattern(RFC5322_FWS_PATTERN)
699
 
            if fws_match:
700
 
                all_qcontent += fws_match.group()
701
 
            qcontent = self._parse_rfc5322_qcontent()
702
 
            if qcontent:
703
 
                all_qcontent += qcontent
704
 
        self._parse_pattern(RFC5322_FWS_PATTERN)
705
 
        if not self._parse_pattern(r'"'):
706
 
            raise SyntaxError('Expected <">', self._parse_text)
707
 
        self._parse_rfc5322_cfws()
708
 
        return all_qcontent
709
 
 
710
 
    def _parse_rfc5322_qcontent(self):
711
 
        qtext_match = self._parse_pattern(r'%s+' % RFC5322_QTEXT_PATTERN)
712
 
        if qtext_match:
713
 
            return qtext_match.group()
714
 
        quoted_pair_match = self._parse_pattern(RFC5322_QUOTED_PAIR_PATTERN)
715
 
        if quoted_pair_match:
716
 
            return quoted_pair_match.group()
717
 
 
718
 
    def _parse_rfc5322_dot_atom(self):
719
 
        self._parse_rfc5322_cfws()
720
 
        dot_atom_text_match = self._parse_pattern(r'%s+(?:\.%s+)*' %
721
 
            (RFC5322_ATEXT_PATTERN, RFC5322_ATEXT_PATTERN))
722
 
        self._parse_rfc5322_cfws()
723
 
        return dot_atom_text_match and dot_atom_text_match.group()
724
 
 
725
 
    def _parse_dot_key_atom(self):
726
 
        # Like _parse_rfc5322_dot_atom, but disallows "/" (forward slash) and
727
 
        # "=" (equal sign).
728
 
        self._parse_rfc5322_cfws()
729
 
        dot_atom_text_match = self._parse_pattern(r'%s+(?:\.%s+)*' %
730
 
            (KTEXT_PATTERN, KTEXT_PATTERN))
731
 
        self._parse_rfc5322_cfws()
732
 
        return dot_atom_text_match and dot_atom_text_match.group()
733
 
 
734
 
    def _parse_key_atom(self):
735
 
        # Like _parse_dot_key_atom, but also disallows "." (dot).
736
 
        self._parse_rfc5322_cfws()
737
 
        dot_atom_text_match = self._parse_pattern(r'%s+' % KTEXT_PATTERN)
738
 
        self._parse_rfc5322_cfws()
739
 
        return dot_atom_text_match and dot_atom_text_match.group()
740
 
 
741
 
    def _parse_rfc5322_cfws(self):
742
 
        fws_match     = False
743
 
        comment_match = True
744
 
        while comment_match:
745
 
            fws_match     = fws_match or self._parse_pattern(RFC5322_FWS_PATTERN)
746
 
            comment_match = self._parse_rfc5322_comment()
747
 
        fws_match = fws_match or self._parse_pattern(RFC5322_FWS_PATTERN)
748
 
        return fws_match or comment_match
749
 
 
750
 
    def _parse_rfc5322_comment(self):
751
 
        if self._parse_pattern(r'\('):
752
 
            while self._parse_pattern(RFC5322_FWS_PATTERN) or self._parse_rfc5322_ccontent(): pass
753
 
            if self._parse_pattern(r'^\)'):
754
 
                return True
755
 
            else:
756
 
                raise SyntaxError('comment: expected FWS or ccontent or ")"', self._parse_text)
757
 
 
758
 
    def _parse_rfc5322_ccontent(self):
759
 
        if self._parse_pattern(r'%s+' % RFC5322_CTEXT_PATTERN):
760
 
            return True
761
 
        elif self._parse_pattern(RFC5322_QUOTED_PAIR_PATTERN):
762
 
            return True
763
 
        elif self._parse_rfc5322_comment():
764
 
            return True
765
 
 
766
 
# Authentication result classes directory
767
 
###############################################################################
768
 
 
769
 
RESULT_CLASSES = [
770
 
    DKIMAuthenticationResult,
771
 
    DomainKeysAuthenticationResult,
772
 
    SPFAuthenticationResult,
773
 
    SenderIDAuthenticationResult,
774
 
    IPRevAuthenticationResult,
775
 
    SMTPAUTHAuthenticationResult
776
 
]
777
 
 
778
 
# vim:sw=4 sts=4