How to convert passwd to userdb for ISPConfig

According to my previous howto regarding ISPConfig and Courier's userdb, there are some problems with some crypted passwords. In particular, passwords may contain the delimiter (|) and the back quote (`) for evaluation in bash shell. As a result, the generated userdb will be invalid. Below is the script to fix these problems.

Note that I wrote below code in pure Python and it shown its high performance for manipulating system configuration.

/usr/local/bin/userdb2ispconfig

#!/usr/bin/env python
 
import re
import os
import pwd
 
SHADOW = '/etc/shadow'
USERDB = '/etc/courier/userdb'
MAKEUSERDB = '/usr/sbin/makeuserdb'
 
class SPwd:
    def __init__(self):
        self.getspall()
 
    def getspnam(self,name):
        return self._dict[name]
 
    def getspall(self):
        self._dict = {}
        self._list = []
        f = open(SHADOW,'r')
        for line in f.readlines():
            v = line.strip().split(':')
            self._dict[v[0]] = v
            self._list.append(v)
        f.close()
        return self._list
 
spwd = SPwd()
 
class Application:
    def __init__(self):
        pass
 
    def run(self):
        lines = []
        for user in pwd.getpwall():
            username,password,uid,gid,gecos,home,shell = user
            shadow = spwd.getspnam(username)[1]
            if home.startswith('/var/www/web'):
                domain,username = username.split('_')
                udb_line = self.generate_line(username,domain,uid,gid,home,shell,password,shadow)
                lines.append(udb_line)
        self.save(lines)
        os.system(MAKEUSERDB)
 
    def save(self,lines):
        os.unlink(USERDB)
        fd = open(USERDB,'w')
        for line in lines:
            fd.write(line+'\n')
        fd.close()
        os.chmod(USERDB,0600)
 
    def generate_line(self,username,domain,uid,gid,home,shell,password,shadow):
        return "%(username)s@%(domain)s\tuid=%(uid)s|gid=%(gid)s|home=%(home)s|shell=%(shell)s|mail=/var/www/www.%(domain)s/user/%(domain)s_%(username)s/Maildir|systempw=%(password)s" % {
            'username': username,
            'domain': domain,
            'uid': uid,
            'gid': gid,
            'home': home,
            'shell': shell,
            'password': shadow}
 
if __name__ == '__main__':
    app = Application()
    app.run()
 

Tags: , ,


updated: 2007/05/08 makeuserdb has been moved to /usr/sbin

Reply