File: Net.py

package info (click to toggle)
mobyle 1.5.5%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,288 kB
  • sloc: python: 22,709; makefile: 35; sh: 33; ansic: 10; xml: 6
file content (402 lines) | stat: -rw-r--r-- 15,093 bytes parent folder | download | duplicates (3)
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
########################################################################################
#                                                                                      #
#   Author: Bertrand Neron,                                                            #
#   Organization:'Biological Software and Databases' Group, Institut Pasteur, Paris.   #  
#   Distributed under GPLv2 Licence. Please refer to the COPYING.LIB document.        #
#                                                                                      #
########################################################################################


import smtplib
import mimetypes 
from email import Encoders
from email.MIMEBase import MIMEBase
from email.MIMEMultipart import MIMEMultipart
from email.MIMEImage import MIMEImage
from email.MIMEText import MIMEText
import re
import os 
import types

from Mobyle.MobyleError import MobyleError , UserValueError , EmailError , TooBigError

import Local.black_list
import Local.Policy
import Local.mailTemplate

from logging import getLogger
n_log = getLogger( __name__ )

from Mobyle.ConfigManager import Config
cfg = Config()


def checkHost( ):

    try:
        userIP = os.environ[ 'REMOTE_ADDR' ]
    except KeyError:
        #MobyleJob is executed from commandline
        return True

    #rewriting the blacklist in a regexp
    pattern = '|'.join( Local.black_list.host )
    pattern = pattern.replace('.' , '\.' )
    pattern = pattern.replace('*' , '.*')
    pattern = "^(%s)$" % pattern
    auto = re.compile( pattern )

    method= 'host'
    if auto.search( userIP ):
        try:
            userMsg = Local.Policy.emailUserMessage( method )
        except:
            userMsg = "you are not allowed to run on this server for now"
        return NetResponse( False, 'IP address is in black list', userMsg, method )
    else:
        return NetResponse( True , '' , '' , method )


class EmailAddress:
        
    INVALID = 0
    VALID = 1
    CONTINUE = 2
    
    def __init__( self , addr ):
        """
        @param addr: the emails addresses
        @type addr: string or list of strings
        """
        if not addr:
            raise MobyleError , 'addr must not be empty'
        if isinstance( addr , types.StringTypes ):
            self.addr = [ addr.strip() ]
        elif isinstance( addr , ( types.ListType , types.TupleType ) ):
            self.addr = addr 
        else:
            raise MobyleError , " addr must be a string or a list of strings : "+ str( addr )
        
        self._methods = [ self._checkSyntax ,
                          self._checkBlackList ,
                          self._checkLocalRules ]      
        
        if cfg.dnsResolver():
            self._methods.append( self._checkDns )
        
        self._message = ''        
    
    def __str__(self):
        return ','.join( self.addr )
    
    def getAddr(self):
        return [ addr for addr in self.addr ]
    
    
    def check( self ):
        """
        check if the addresses are valid:
         right syntax, 
         not in black list, 
         according to the local rules 
         and the domain have a mx fields)
        @return: True if the addresses pass the controls, False otherwise
        @rtype: boolean  
        """
        self._message = ''
        for oneAddr in self.addr:
            for method in self._methods:
                rep = method( oneAddr )
                if rep.status == self.CONTINUE :
                    continue
                elif rep.status == self.VALID or rep.status == self.INVALID:
                    return rep
                else:
                    raise MobyleError , method.__name__+ " return an invalid response :"+ str(rep)
        return NetResponse( self.VALID , '' , '' , '' )


    def getMessage( self ):
        return self._message
            
    def _checkSyntax( self , addr ):
        method =  'syntax'
        email_pat = re.compile( "^[a-z0-9\-\.+_]+\@([a-z0-9\-]+\.)+([a-z]){2,4}$" , re.IGNORECASE )
        match = re.match( email_pat , addr )

        if match is None:
            try:
                userMsg = Local.Policy.emailUserMessage( method )
            except:
                userMsg = "you are not allowed to run on this server for now"
            return NetResponse(self.INVALID , "invalid syntax for email address" , userMsg,  method )
        else:
            return NetResponse(self.CONTINUE , '' , '' ,  method )

    def checkBlackList( self ):
        for oneAddr in self.addr:
            rep = self._checkBlackList( oneAddr )
            if rep.status == self.CONTINUE :
                continue
            elif rep.status == self.VALID or rep.status == self.INVALID:
                return rep
        return NetResponse( self.VALID , '' , '' , '' )
        
    def _checkBlackList( self , addr ):
        pattern = '|'.join( Local.black_list.users )
        pattern = pattern.replace('.' , '\.' )
        pattern = pattern.replace('*' , '.*')
        pattern = "^(%s)$" % pattern
        auto = re.compile( pattern )
        method = 'blackList'
        if auto.search( addr ):
            try:
                userMsg = Local.Policy.emailUserMessage( method )
            except:
                userMsg = "you are not allowed to run on this server for now"
            return NetResponse(self.INVALID , "email is in black_list" , userMsg , method )
        else:
            return NetResponse(self.CONTINUE , '' , '' , method )


    def _checkLocalRules( self, addr  ):
        #tester si le module existe ? existe toujours meme si vide ?
        rep = Local.Policy.emailCheck( email = addr )
        message = ''
        userMsg = ''
        method = 'LocalRules'
        if rep == self.INVALID:
            message = "email is rejected by our local policy"
            try:
                userMsg = Local.Policy.emailUserMessage( method )
            except:
                userMsg = "you are not allowed to run on this server for now"
        return NetResponse( rep , message , userMsg , method)


    def _checkDns( self , addr ):
        import dns.resolver
        method = 'dns'
        try:
            userMsg = Local.Policy.emailUserMessage( method )
        except:
            userMsg = "you are not allowed to run on this server for now"
        user , domainName  = addr.split('@')
        try:
            answers = dns.resolver.query( domainName , 'MX')
        except dns.resolver.NXDOMAIN , err :
            self._message = "unknown name domain"
            return NetResponse( self.INVALID , "unknown name domain" , userMsg , method )
        except dns.resolver.NoAnswer ,err :
            try:
                answers = dns.resolver.query( domainName , 'A')
            except:
                return NetResponse( self.INVALID , "no mail server" , userMsg , method )
            return NetResponse( self.CONTINUE , '' , '' , method )
        except (dns.name.EmptyLabel, dns.resolver.NoNameservers):
            return NetResponse( self.INVALID , "no domain name server" , userMsg , method )
        except dns.exception.Timeout:
            return NetResponse( self.CONTINUE, "dns timeout" , userMsg , method )
        except Exception, err:
            msg = "unexpected error in  Email._checkDns : "+ str( err )
            n_log.critical( msg, exc_info = True )
            return NetResponse( self.INVALID , msg , userMsg , method )
        return NetResponse( self.CONTINUE , '' , '' , method )
            
class NetResponse:
    
    def __init__(self, status , message , user_message , method ):
        """ """
        self.status = status
        """Internal Mobyle Message"""
        self.message = message
        """User Message"""
        self.user_message = user_message
        """the name of the method"""
        self.method = method
    

        
class Email: 
    
    def __init__( self , To , cc = None): 
        """
        @param To: the recipients addresses
        @type To: EmailAddress instance
        @param cc: the emails adresses in copy of this mail
        @type cc: EmailAddress instance
        """   
        self.To = To
        self.cc = cc
        self.mailhost = cfg.mailhost()
        self.headers = None
        self.body = None
        
    def getBody(self):
        return self.body
    
    def getHeaders(self):
        return self.headers
    
    def send( self , templateName , dict ,  files= None  ):
        """
        send an email to the Email.To recipients, using the template to build email body.
        @param template: the template of the mail. see Local/mailTemplate.py
        @type template: string 
        @param dict: the dictionnary used to expend the template
        @type dict: dictionnary
        @param files: the list of file names to attach to this email
        @type files: list of strings 
        """
        try:
            template = getattr( Local.mailTemplate , templateName )
        except AttributeError ,err:
            msg = "error during template %s loading: err" %( templateName ,err )
            n_log.critical( msg )
            raise MobyleError , err 
        try:
            mail = template % dict
        except ( TypeError , KeyError ) , err:
            msg = "error during template %s expanding: %s. This mail sending is aborted" %( templateName , err )
            n_log.critical( msg )
            raise MobyleError , msg 
        if not mail and not files:
            errMsg = "no msg and no files for template %s send email aborted" % templateName
            n_log.warning( errMsg )
            return None
        
        mailAttr , msg = self._parse( mail )
        if files:
            emailBody = MIMEMultipart()
        else:
            emailBody = MIMEText( msg , 'plain' , 'utf-8')
        
        mailAttr[ 'To' ] = str( self.To )
        recipients = self.To.getAddr()
        if self.cc:
            recipients.extend( self.cc.getAddr() )
            emailBody[ 'Cc' ] = str( self.cc )
            
        for attr in mailAttr.keys():
            if attr == 'Reply-To':
                emailBody[ attr ] = ', '.join( mailAttr[ attr ] )
            elif attr == 'Cc' or attr == 'Bcc' :
                recipients.extend( mailAttr[ attr ] )
                emailBody[ attr ] = ', '.join( mailAttr[ attr ] )
            else:
                emailBody[ attr ] =  mailAttr[ attr ] 
        try:
            try:
                s = smtplib.SMTP( self.mailhost, timeout= 30 )
            except TypeError:
                # timeout was added in python 2.6
                s = smtplib.SMTP( self.mailhost )
        except Exception, err:
            #except smtplib.SMTPException , err:
            n_log.error( "can't connect to mailhost \"%s\"( check Local.Config.Config.py ): %s" %( self.mailhost , err ) )
            raise EmailError , err
        
        if files:
            emailBody.preamble = 'You will not see this in a MIME-aware mail reader.\n'
            # To guarantee the message ends with a newline
            emailBody.epilogue = ''            
            if msg :
                msg = MIMEText( msg  , 'plain' , 'utf-8' )
                emailBody.attach( msg )

            for filename in files:
                if not os.path.isfile( filename ):
                    continue
    
                # Guess the content type based on the file's extension.  Encoding
                # will be ignored, although we should check for simple things like
                # gzip'd or compressed files.
    
                ctype, encoding = mimetypes.guess_type( filename )
                if ctype is None or encoding is not None:
                    # No guess could be made, or the file is encoded (compressed), so
                    # use a generic bag-of-bits type.
                    ctype = 'application/octet-stream'
    
                maintype, subtype = ctype.split( '/' , 1 )
                if maintype == 'text':
                    fp = open( filename , 'r')
                    # Note: we should handle calculating the charset
                    msg = MIMEText( fp.read() , _subtype = subtype )
                    fp.close()
                elif maintype == 'image':
                    fp = open( filename , 'rb' )
                    msg = MIMEImage( fp.read() , _subtype = subtype )
                    fp.close()
                else:
                    if filename == 'index.xml':
                        fp = self._indexPostProcess()
                    else:
                        fp = open( filename , 'rb' )
                        
                    msg = MIMEBase( maintype , subtype )
                    msg.set_payload( fp.read() )
                    fp.close()
                    # Encode the payload using Base64
                    Encoders.encode_base64( msg )
    
                # Set the filename parameter
                msg.add_header( 'Content-Disposition' , 'attachment', filename = os.path.basename( filename) )
                emailBody.attach( msg )
        
        try:
            s.sendmail( mailAttr[ 'From' ] , recipients , emailBody.as_string() )
            s.quit()
        except smtplib.SMTPSenderRefused, err:
            if err.smtp_code == 552 :
                raise TooBigError , str( err )
            else:
                raise EmailError , err
        except smtplib.SMTPException , err:
            n_log.error( "can't send email : %s" % err )
            raise EmailError , err
                
        except Exception , err:    
            #except smtplib.SMTPException , err:
            n_log.error( "can't send email : %s" % err , exc_info = True)
            n_log.error( str( recipients ) )
            raise EmailError , err                
            
    def _parse(self , mail ):
        headers = {}
        mail = mail.split( '\n' )
        iterator = iter( mail )
        begin = False

        for line in iterator:
            if not (begin or line):
                begin = True
                continue

            splitedLine = line.split(':')
            fields = splitedLine[0].strip()
            value = ':'.join( splitedLine[1:] )
            if fields == 'From':
                value = value.split( ',' )
                try:
                    value = value[0].strip()
                except IndexError :
                    raise MobyleError, '"From:" field cannot be empty '
                if not value:
                    raise MobyleError, '"From:" field cannot be empty '
            elif fields in ( 'Reply-To' , 'Cc' , 'Bcc' ) :
                value = value.split( ',' )
                value = [ val.strip() for val in value if val.strip() ]
                if not value:
                    continue
            elif not fields:
                body = '\n'.join( iterator )
                break
            else:
                value = value.strip()
                if not value:
                    continue
            headers[ fields ] = value
        self.headers = headers
        self.body = body
        return   headers , body