Update of /cvsroot/tmda/tmda/TMDA
In directory sc8-pr-cvs1:/tmp/cvs-serv27648

Modified Files:
        Auth.py Errors.py 
Log Message:
Added Errors.AuthError.
Fixed bugs in Auth.py so checkpassword and imap authentication works.


Index: Auth.py
===================================================================
RCS file: /cvsroot/tmda/tmda/TMDA/Auth.py,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- Auth.py     21 Dec 2002 10:49:28 -0000      1.2
+++ Auth.py     1 Apr 2003 17:24:22 -0000       1.3
@@ -30,7 +30,7 @@
 
 import Version
 import Util
-
+import Errors
 
 ## FIXME: debug stuff should be in it's own module
 class Devnull:
@@ -230,9 +230,13 @@
 
 def run_authprog(username, password):
     """authprog should return 0 for auth ok, and a positive integer in
-    case of a problem."""
+    case of a problem.  Return 1 upon successful authentication, and 0
+    otherwise."""
     print >> DEBUGSTREAM, "Trying authprog method"
-    return pipecmd('%s' % authprog, '%s\0%s\0' % (username, password))
+    cmd = "/bin/sh -c 'exec %s 3<&0'" % authprog
+    authResult = pipecmd(cmd, '%s\0%s\0' % (username, password))
+    print >> DEBUGSTREAM, "'%s' returned %d" % (authprog, authResult)
+    return authResult == 0
 
 def run_remoteauth(username, password):
     """Authenticate username/password combination against a remote
@@ -248,13 +252,15 @@
             port = int(remoteauth['port'])
         M = imaplib.IMAP4(remoteauth['host'], port)
         try:
-            M.login(username, password)
-            M.logout()
-            M.close()
-            return 1
-        except:
+            (type, data) = M.login(username, password)
+            print >> DEBUGSTREAM, "Login response: %s %s" % (type, data)
+            retVal = ( type == 'OK' )
+            (type, data) = M.logout()
+            print >> DEBUGSTREAM, "Logout response: %s %s" % (type, data)
+            return retVal
+        except IMAP4.error, err:
             print >> DEBUGSTREAM, "imap authentication for [EMAIL PROTECTED] failed" 
% \
-                  (username, remoteauth['host'])
+                  (username, remoteauth['host'], err)
             return 0
     elif remoteauth['proto'] == 'imaps':
         import imaplib
@@ -262,12 +268,16 @@
             port = int(remoteauth['port'])
         M = IMAP4_SSL(remoteauth['host'], port)
         try:
-            M.login(username, password)
+            (type, data) = M.login(username, password)
+            print >> DEBUGSTREAM, "Login response: %s %s" % (type, data)
+            retVal = ( type == 'OK' )
             M.logout()
-            return 1
-        except:
-            print >> DEBUGSTREAM, "imaps authentication for [EMAIL PROTECTED] failed" 
% \
-                  (username, remoteauth['host'])
+            (type, data) = M.logout()
+            print >> DEBUGSTREAM, "Logout response: %s %s" % (type, data)
+            return retVal
+        except IMAP4_SSL.error, err:
+            print >> DEBUGSTREAM, "imaps authentication for [EMAIL PROTECTED] failed: 
%s" % \
+                  (username, remoteauth['host'], err)
             return 0
     elif remoteauth['proto'] in ('pop3', 'apop'):
         import poplib
@@ -284,9 +294,9 @@
                 M.apop(username, password)
                 M.quit()
                 return 1
-        except:
-            print >> DEBUGSTREAM, "%s authentication for [EMAIL PROTECTED] failed" % \
-                  (remoteauth['proto'], username, remoteauth['host'])
+        except poplib.error_proto, err:
+            print >> DEBUGSTREAM, "%s authentication for [EMAIL PROTECTED] failed: 
%s" % \
+                  (remoteauth['proto'], username, remoteauth['host'], err)
             return 0
     elif remoteauth['proto'] == 'ldap':
         import ldap
@@ -310,15 +320,20 @@
 def authenticate_plain(username, password, type=None):
     if type == None:
         type = authtype
-    if type == 'remote':
-        return run_remoteauth(username, password)
-    if type == 'prog':
-        return run_authprog(username, password)
-    if type == 'file':
-        ## FIXME: implement /etc/tofmipd auth
-        return 0
+    try:
+      if type == 'remote':
+          return run_remoteauth(username, password)
+      if type == 'prog':
+          return run_authprog(username, password)
+      if type == 'file':
+          ## FIXME: implement /etc/tofmipd auth
+          return 0
+    except:
+      return 0
     
-    raise AuthError, "Unknown authentication type '%s'." % type
+    raise Errors.AuthError, \
+      "Unknown authentication type '%s'." % type, \
+      "Ensure that Auth.authtype is set to 'remote' 'prog' or 'file'"
 
 def authfile2dict(authfile):
     """Iterate over a tmda-ofmipd authentication file, and return a

Index: Errors.py
===================================================================
RCS file: /cvsroot/tmda/tmda/TMDA/Errors.py,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- Errors.py   6 Dec 2002 20:32:38 -0000       1.8
+++ Errors.py   1 Apr 2003 17:24:22 -0000       1.9
@@ -67,3 +67,15 @@
 
 class MessageError(QueueError):
     pass
+
+class AuthError(TMDAError):
+    """Authentication Errors""" 
+    def __init__(self, errmsg = 'Authentication Error', helpmsg = ''):
+        self.msg = errmsg
+        self.help = helpmsg
+
+    def __repr__(self):
+        if self.help == '':
+          return '%s: %s' % (self.__class__, self.msg)
+        else:
+          return '%s: %s\n(%s)' % (self.__class__, self.msg, self.help)

_______________________________________
tmda-cvs mailing list
http://tmda.net/lists/listinfo/tmda-cvs

Reply via email to