I'm using TMDA with the Courier MTA, and all my users get virtual email
directories as implemented in the Courier manner.  On my system, all of
these are subdirectories which live under /var/vmail, and all are owned
by uid=vmail and gid=vmail.  The .tmda and Maildir directories live in
this subdirectory.  For example, for the virtual email address
"[EMAIL PROTECTED]", we have the following items:

  /var/vmail/[EMAIL PROTECTED]          - the top-level email directory
  /var/vmail/[EMAIL PROTECTED]/.tmda    - the associated tmda home
  /var/vmail/[EMAIL PROTECTED]/Maildir  - the associated Maildir

I can make use of the --vhost-script option for tmda-filter and
tmda-ofmipd to cause each of these programs to set HOME to the
appropriate virtual directory, but I am unable to perform the
corresponding function with tmda-pending, which lacks the --vhost-script
option.

Furthermore, the method in http://wiki.tmda.net/VirtualDomainsHowto for
using tmda-pending with virtual domains does not work for my Courier
virtual email setup ... tmda-pending still can't find the pending
directory.

Up until now, I have always manually hacked the TMDA source code to
allow my users to utilize tmda-pending with their Courier virtual mail
directories.  However, I'm getting tired of having to reinstall this
hack every time I upgrade TMDA.

Therefore, I have implemented a patch for tmda-pending which also gives
it a --vhost-script option.  It works the same as it does for
tmda-filter and tmda-ofmipd.  The script specified by this option will
be passed the user name and the domain name as determined with the
following algorithm (which makes use of two other new options:
--vhost-user and --vhost-domain) ...

  user name:   if the --vhost-user option is set, use it

               else if the USER environment variable is set,
               use it

               else if the LOGNAME environment variable is set,
               use it

               else ERROR

  domain name: if the --vhost-domain option is set, use it

               else if socket.gethostbyaddr(socket.gethostname())[0]
               succeeds, use this value

               else ERROR

I have tested this, and it works fine for me, although I wouldn't be
surprised if I have overlooked certain test cases, so please feel free
to improve this code.

I sincerely hope that this feature can make it into the next TMDA
release.

This patch is based on tmda-1.1.9:

--- tmda-pending.orig	2006-12-18 16:19:03.000000000 -0500
+++ tmda-pending	2006-12-18 16:19:20.000000000 -0500
@@ -1,3 +1,4 @@
 #!/usr/bin/python2.4
+# -*- python -*-
 #
 # Copyright (C) 2001-2006 Jason R. Mastaler <[EMAIL PROTECTED]>
@@ -25,4 +26,5 @@
 import os
 import sys
+import socket
 
 try:
@@ -72,4 +74,7 @@
   $ tmda-pending -T -b
 
+  (same as above, but use /usr/local/bin/vhome as the vhome-script)
+  $ tmda-pending -T -b --vhome-script=/usr/local/bin/vhome
+
   (interactively operate on just these messages)
   $ tmda-pending 1012182077.5803 1012939546.7870
@@ -101,4 +106,31 @@
 
 # general
+gengroup.add_option("--vhome-script",
+	             metavar="SCRIPT", dest="vhomescript",
+        	     help= \
+"""Full pathname of SCRIPT that prints a virtual email user's home
+directory on standard output.  tmda-pending will read that path and
+set $HOME to that path so that '~' expansion works properly for
+virtual users.  The script takes two arguments, the user name and
+the domain, on its command line.  This option is for use with the
+VPopMail and VMailMgr add-ons to qmail and with other systems that
+manage virtual email addresses.  The user name is obtained from the
+USER and LOGNAME environment variables, in that order, or by the
+value of the '--vhome-user' option, if defined. The domain name is
+obtained by socket.gethostbyaddr(socket.gethostname())[0], or by
+the '--vhome-domain' option, if defined.""")
+
+gengroup.add_option("--vhome-user",
+	             metavar="USER", dest="vhomeuser",
+        	     help= \
+"""The user name to optionally pass to the program specified
+in the '--vhome-script' option, above.""")
+
+gengroup.add_option("--vhome-domain",
+	             metavar="DOMAIN", dest="vhomedomain",
+        	     help= \
+"""The domain name to optionally pass to the program specified
+in the '--vhome-script' option, above.""")
+
 gengroup.add_option("-c", "--config-file",
                     metavar="FILE", dest="config_file",
@@ -230,4 +262,42 @@
     threshold = None
 
+if opts.vhomescript:
+    """Set $HOME to the recipient's (virtual user) home directory."""
+    user = None
+    if opts.vhomeuser:
+	user = opts.vhomeuser
+    else:
+	try:
+            user = os.environ['USER']
+        except:
+            try:
+                user = os.environ['LOGNAME']
+            except:
+                user = None
+    domain = None
+    if opts.vhomedomain:
+        domain = opts.vhomedomain
+    else:
+        try:
+            domain = socket.gethostbyaddr(socket.gethostname())[0]
+        except:
+            domain = None
+    message = ''
+    if user is None:
+        message += ' USER'
+    if domain is None:
+        message += ' DOMAIN'
+    if message != '':
+        sys.stdout.write('missing:%s\n' % message)
+        sys.exit(1)
+    cmd = "%s '%s' '%s'" % ( opts.vhomescript, user, domain )
+    fpin = os.popen(cmd)
+    vuserhomedir = fpin.read().strip()
+    if fpin.close() is None:
+        os.environ['HOME'] = vuserhomedir
+        os.chdir(vuserhomedir)
+    else:
+        sys.stdout.write('invocation failed: %s\n' % ( cmd, ))
+        sys.exit(1)
 
 from TMDA import Pending


-- 
 Lloyd Zusman
 [EMAIL PROTECTED]
 God bless you.
_________________________________________________
tmda-workers mailing list ([email protected])
http://tmda.net/lists/listinfo/tmda-workers

Reply via email to