Advertisement
Guest User

Untitled

a guest
Jan 11th, 2013
1,189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.37 KB | None | 0 0
  1. import smtplib
  2. import socket
  3. import re
  4. import email.utils
  5. import base64
  6. import hmac
  7. from email.base64mime import encode as encode_base64
  8. from sys import stderr
  9.  
  10. class PlainSmtpSSL(smtplib.SMTP_SSL):
  11.     def login(self, user, password):
  12.         """Log in on an SMTP server that requires authentication.
  13.  
  14.        The arguments are:
  15.            - user:     The user name to authenticate with.
  16.            - password: The password for the authentication.
  17.  
  18.        If there has been no previous EHLO or HELO command this session, this
  19.        method tries ESMTP EHLO first.
  20.  
  21.        This method will return normally if the authentication was successful.
  22.  
  23.        This method may raise the following exceptions:
  24.  
  25.         SMTPHeloError            The server didn't reply properly to
  26.                                  the helo greeting.
  27.         SMTPAuthenticationError  The server didn't accept the username/
  28.                                  password combination.
  29.         SMTPException            No suitable authentication method was
  30.                                  found.
  31.        """
  32.  
  33.         def encode_cram_md5(challenge, user, password):
  34.             challenge = base64.decodestring(challenge)
  35.             response = user + " " + hmac.HMAC(password, challenge).hexdigest()
  36.             return encode_base64(response, eol="")
  37.  
  38.         def encode_plain(user, password):
  39.             return encode_base64("\0%s\0%s" % (user, password), eol="")
  40.  
  41.  
  42.         AUTH_PLAIN = "PLAIN"
  43.         AUTH_CRAM_MD5 = "CRAM-MD5"
  44.         AUTH_LOGIN = "LOGIN"
  45.  
  46.         self.ehlo_or_helo_if_needed()
  47.  
  48.         if not self.has_extn("auth"):
  49.             raise SMTPException("SMTP AUTH extension not supported by server.")
  50.  
  51.         # Authentication methods the server supports:
  52.         authlist = self.esmtp_features["auth"].split()
  53.  
  54.         # List of authentication methods we support: from preferred to
  55.         # less preferred methods. Except for the purpose of testing the weaker
  56.         # ones, we prefer stronger methods like CRAM-MD5:
  57.         preferred_auths = [AUTH_PLAIN, AUTH_LOGIN]
  58.  
  59.         # Determine the authentication method we'll use
  60.         authmethod = None
  61.         for method in preferred_auths:
  62.             if method in authlist:
  63.                 authmethod = method
  64.                 break
  65.  
  66.         if authmethod == AUTH_CRAM_MD5:
  67.             (code, resp) = self.docmd("AUTH", AUTH_CRAM_MD5)
  68.             if code == 503:
  69.                 # 503 == 'Error: already authenticated'
  70.                 return (code, resp)
  71.             (code, resp) = self.docmd(encode_cram_md5(resp, user, password))
  72.         elif authmethod == AUTH_PLAIN:
  73.             (code, resp) = self.docmd("AUTH",
  74.                 AUTH_PLAIN + " " + encode_plain(user, password))
  75.         elif authmethod == AUTH_LOGIN:
  76.             (code, resp) = self.docmd("AUTH",
  77.                 "%s %s" % (AUTH_LOGIN, encode_base64(user, eol="")))
  78.             if code != 334:
  79.                 raise SMTPAuthenticationError(code, resp)
  80.             (code, resp) = self.docmd(encode_base64(password, eol=""))
  81.         elif authmethod is None:
  82.             raise SMTPException("No suitable authentication method found.")
  83.         if code not in (235, 503):
  84.             # 235 == 'Authentication successful'
  85.             # 503 == 'Error: already authenticated'
  86.             raise SMTPAuthenticationError(code, resp)
  87.         return (code, resp)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement