changeset 3479:6e453ec5d42f

refactored smb mount pseudo-auth, move configuration values to auth object initialization
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 05 Apr 2008 19:19:21 +0200
parents 242d882d9ba4
children b318efb0136b
files MoinMoin/auth/smb_mount.py wiki/config/more_samples/smb_wikiconfig_snippet.py
diffstat 2 files changed, 64 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/auth/smb_mount.py	Sat Apr 05 18:26:33 2008 +0200
+++ b/MoinMoin/auth/smb_mount.py	Sat Apr 05 19:19:21 2008 +0200
@@ -17,32 +17,44 @@
 from MoinMoin.auth import BaseAuth, CancelLogin, ContinueLogin
 
 class SMBMount(BaseAuth):
-    """ auth plugin for (un)mounting an smb share """
-    def __init__(self, smb_dir_user, smb_mountpoint_fn, smb_domain, smb_server,
-                 smb_share, smb_dir_mode, smb_file_mode, smb_iocharset,
-                 smb_log, smb_coding, verbose=False):
+    """ auth plugin for (un)mounting an smb share,
+        this is a wrapper around mount.cifs -o <options> //server/share mountpoint
+
+        See man mount.cifs for details.
+    """
+    def __init__(self,
+        server, # mount.cifs //server/share
+        share, # mount.cifs //server/share
+        mountpoint_fn, # function of username to determine the mountpoint, e.g.:
+                       # lambda username: u'/mnt/wiki/%s' % username
+        dir_user, # username to get the uid that is used for mount.cifs -o uid=... (e.g. 'www-data')
+        domain, # mount.cifs -o domain=...
+        dir_mode='0700', # mount.cifs -o dir_mode=...
+        file_mode='0600', # mount.cifs -o file_mode=...
+        iocharset='utf-8', # mount.cifs -o iocharset=... (try 'iso8859-1' if default does not work)
+        coding='utf-8', # encoding used for username/password/cmdline (try 'iso8859-1' if default does not work)
+        log='/dev/null', # logfile for mount.cifs output
+        ):
         BaseAuth.__init__(self)
-        self.verbose = verbose
-        self.smb_dir_user = smb_dir_user
-        self.smb_mountpoint_fn = smb_mountpoint_fn
-        self.smb_domain = smb_domain
-        self.smb_server = smb_server
-        self.smb_share = smb_share
-        self.smb_dir_mode = smb_dir_mode
-        self.smb_file_mode = smb_file_mode
-        self.smb_iocharset = smb_iocharset
-        self.smb_log = smb_log
-        self.smb_coding = smb_coding
+        self.server = server
+        self.share = share
+        self.mountpoint_fn = mountpoint_fn
+        self.dir_user = dir_user
+        self.domain = domain
+        self.dir_mode = dir_mode
+        self.file_mode = file_mode
+        self.iocharset = iocharset
+        self.log = log
+        self.coding = coding
 
     def do_smb(self, request, username, password, login):
-        verbose = self.verbose
-        if verbose: logging.info("login=%s logout=%s: got name=%s" % (login, not login, username))
+        logging.debug("login=%s logout=%s: got name=%s" % (login, not login, username))
 
         import os, pwd, subprocess
-        web_username = self.smb_dir_user
+        web_username = self.dir_user
         web_uid = pwd.getpwnam(web_username)[2] # XXX better just use current uid?
 
-        mountpoint = self.smb_mountpoint_fn(username)
+        mountpoint = self.mountpoint_fn(username)
         if login:
             cmd = u"sudo mount -t cifs -o user=%(user)s,domain=%(domain)s,uid=%(uid)d,dir_mode=%(dir_mode)s,file_mode=%(file_mode)s,iocharset=%(iocharset)s //%(server)s/%(share)s %(mountpoint)s >>%(log)s 2>&1"
         else:
@@ -51,14 +63,14 @@
         cmd = cmd % {
             'user': username,
             'uid': web_uid,
-            'domain': self.smb_domain,
-            'server': self.smb_server,
-            'share': self.smb_share,
+            'domain': self.domain,
+            'server': self.server,
+            'share': self.share,
             'mountpoint': mountpoint,
-            'dir_mode': self.smb_dir_mode,
-            'file_mode': self.smb_file_mode,
-            'iocharset': self.smb_iocharset,
-            'log': self.smb_log,
+            'dir_mode': self.dir_mode,
+            'file_mode': self.file_mode,
+            'iocharset': self.iocharset,
+            'log': self.log,
         }
         env = os.environ.copy()
         if login:
@@ -67,8 +79,8 @@
                     os.makedirs(mountpoint) # the dir containing the mountpoint must be writeable for us!
             except OSError:
                 pass
-            env['PASSWD'] = password.encode(self.smb_coding)
-        subprocess.call(cmd.encode(self.smb_coding), env=env, shell=True)
+            env['PASSWD'] = password.encode(self.coding)
+        subprocess.call(cmd.encode(self.coding), env=env, shell=True)
 
     def login(self, request, user_obj, **kw):
         username = kw.get('username')
@@ -81,3 +93,4 @@
         if user_obj and not user_obj.valid:
             self.do_smb(request, user_obj.name, None, False)
         return user_obj, True
+
--- a/wiki/config/more_samples/smb_wikiconfig_snippet.py	Sat Apr 05 18:26:33 2008 +0200
+++ b/wiki/config/more_samples/smb_wikiconfig_snippet.py	Sat Apr 05 19:19:21 2008 +0200
@@ -1,15 +1,26 @@
-    # TODO: needs to get fixed for 1.7 auth objects
+    # This is a sample configuration snippet that shows how to use the smb
+    # mount plugin. SMBMount is only useful for very special applications
+    # (and requires more code to be useful).
+    # If you don't understand it, you don't need it.
 
-    smb_server = "smb.example.org" # smb server name
-    smb_domain = 'DOMAIN' # smb domain name
-    smb_share = 'FILESHARE' # smb share we mount
-    smb_mountpoint = u'/mnt/wiki/%(username)s' # where we mount the smb filesystem
+    from MoinMoin.auth.smb_mount import SMBMount
+
+    smbmounter = SMBMount(
+        # you may remove default values if you are happy with them
+        # see man mount.cifs for details
+        server='smb.example.org', # (no default) mount.cifs //server/share
+        share='FILESHARE', # (no default) mount.cifs //server/share
+        mountpoint_fn=lambda username: u'/mnt/wiki/%s' % username, # (no default) function of username to determine the mountpoint
+        dir_user='www-data', # (no default) username to get the uid that is used for mount.cifs -o uid=...
+        domain='DOMAIN', # (no default) mount.cifs -o domain=...
+        dir_mode='0700', # (default) mount.cifs -o dir_mode=...
+        file_mode='0600', # (default) mount.cifs -o file_mode=...
+        iocharset='utf-8', # (default) mount.cifs -o iocharset=... (try 'iso8859-1' if default does not work)
+        coding='utf-8', # (default) encoding used for username/password/cmdline (try 'iso8859-1' if default does not work)
+        log='/dev/null', # (default) logfile for mount.cifs output
+    )
+
+    auth = [....., smbmounter] # you need a real auth object in the list before smbmounter
+
     smb_display_prefix = u"S:" # where //server/share is usually mounted for your windows users (display purposes only)
-    smb_dir_user = "wwwrun" # owner of the mounted directories
-    smb_dir_mode = "0700" # mode of the mounted directories
-    smb_file_mode = "0600" # mode of the mounted files
-    smb_iocharset = "iso8859-1" # "UTF-8" > cannot access needed shared library!
-    smb_coding = 'iso8859-1' # coding used for encoding the commandline for the mount command
-    smb_verbose = True # if True, put SMB debug info into log
-    smb_log = "/dev/null" # where we redirect mount command output to