also produce dbm files additionaly to cdb
authorMartin Zobel-Helas <zobel@debian.org>
Sun, 29 Dec 2013 17:21:01 +0000 (18:21 +0100)
committerMartin Zobel-Helas <zobel@debian.org>
Sun, 29 Dec 2013 17:21:01 +0000 (18:21 +0100)
We need to migrate from CDB to DBM, as there is no python-cdb in Debian
stable any more. Provide both file formats, so we can migrate from CDB
to DBM painlessly.

Signed-off-by: Martin Zobel-Helas <zobel@debian.org>
ud-generate

index 5b64a84..1a22954 100755 (executable)
@@ -28,7 +28,7 @@
 #   along with this program; if not, write to the Free Software
 #   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-import string, re, time, ldap, optparse, sys, os, pwd, posix, socket, base64, hashlib, shutil, errno, tarfile, grp, fcntl
+import string, re, time, ldap, optparse, sys, os, pwd, posix, socket, base64, hashlib, shutil, errno, tarfile, grp, fcntl, dbm
 from userdir_ldap import *
 from userdir_exceptions import *
 import UDLdap
@@ -645,6 +645,35 @@ def GenCDB(accounts, File, key):
    if Fdb.close() != None:
       raise "cdbmake gave an error"
 
+def GenDBM(accounts, File, key):
+   Fdb = None
+   try:
+      OldMask = os.umask(0022)
+      # nothing else does the fsync stuff, so why do it here?
+      fn = os.path.join(prefix,File).encode('ascii', 'ignore')
+      fntmp = os.path.join(prefix,File + '.tmp').encode('ascii', 'ignore')
+      try:
+          posix.remove(fntmp)
+      except:
+          pass
+
+      Fdb = dbm.open(fntmp, "c")
+      os.umask(OldMask)
+
+      # Write out the email address for each user
+      for a in accounts:
+         if not key in a: continue
+         value = a[key]
+         user = a['uid']
+         Fdb[user] = value
+
+      Fdb.close()
+      posix.rename(fntmp,fn)
+   # Oops, something unspeakable happened.
+   except:
+      Fdb.close()
+      raise
+
 # Generate the anon XEarth marker file
 def GenMarkers(accounts, File):
    F = None
@@ -1169,7 +1198,9 @@ def generate_all(global_dir, ldap_conn):
 
    GenMailDisable(accounts, global_dir + "mail-disable")
    GenCDB(accounts, global_dir + "mail-forward.cdb", 'emailForward')
+   GenDBM(accounts, global_dir + "mail-forward.dbm", 'emailForward')
    GenCDB(accounts, global_dir + "mail-contentinspectionaction.cdb", 'mailContentInspectionAction')
+   GenDBM(accounts, global_dir + "mail-contentinspectionaction.dbm", 'mailContentInspectionAction')
    GenPrivate(accounts, global_dir + "debian-private")
    GenSSHKnown(host_attrs, global_dir+"authorized_keys", 'authorized_keys', global_dir+'ud-generate.lock')
    GenMailBool(accounts, global_dir + "mail-greylist", "mailGreylisting")
@@ -1252,7 +1283,9 @@ def generate_host(host, global_dir, all_accounts, all_hosts, ssh_userkeys):
    if not 'NOMARKERS' in ExtraList:
       DoLink(global_dir, OutDir, "markers")
    DoLink(global_dir, OutDir, "mail-forward.cdb")
+   DoLink(global_dir, OutDir, "mail-forward.dbm")
    DoLink(global_dir, OutDir, "mail-contentinspectionaction.cdb")
+   DoLink(global_dir, OutDir, "mail-contentinspectionaction.dbm")
    DoLink(global_dir, OutDir, "mail-disable")
    DoLink(global_dir, OutDir, "mail-greylist")
    DoLink(global_dir, OutDir, "mail-callout")
@@ -1261,8 +1294,11 @@ def generate_host(host, global_dir, all_accounts, all_hosts, ssh_userkeys):
    DoLink(global_dir, OutDir, "mail-whitelist")
    DoLink(global_dir, OutDir, "all-accounts.json")
    GenCDB(accounts, OutDir + "user-forward.cdb", 'emailForward')
+   GenDBM(accounts, OutDir + "user-forward.dbm", 'emailForward')
    GenCDB(accounts, OutDir + "batv-tokens.cdb", 'bATVToken')
+   GenDBM(accounts, OutDir + "batv-tokens.dbm", 'bATVToken')
    GenCDB(accounts, OutDir + "default-mail-options.cdb", 'mailDefaultOptions')
+   GenDBM(accounts, OutDir + "default-mail-options.dbm", 'mailDefaultOptions')
 
    # Compatibility.
    DoLink(global_dir, OutDir, "forward-alias")