#!/usr/bin/python # (c) 2002 Michael Fischer v. Mollard (mfvm AT gmx DOT de) # # A little bit to trivial for a big license, so do what you want with # it, but remember: # This script is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. import getopt, sys, os from MySQLdb import connect # Some global variable # You may want to customize this MAILDIRMAKE = "/usr/bin/maildirmake" GID = 8 UID = 8 DATABASE = 'maildb' DBUSER = 'mail' DBPASSWORD = 'secret' def main(): try: opts, args = getopt.getopt(sys.argv[1:], "hd:u:p:n:N:S:H:q:s:", ["help", "database=", "dbuser=", "dbpassword=", "newuser=", "secret=", "fullname=", "home=", "quota=", "shared="]) except getopt.GetoptError: # print help information and exit: usage() sys.exit(2) database, dbuser, dbpassword, newuser, secret, fullname, home, maildir, quota, shared = None, None, None, None, None, None, None, None, None, None for o, a in opts: if o in ("-h", "--help"): usage() sys.exit() elif o in ("-d", "--database"): database = a elif o in ("-u", "--dbuser"): dbuser = a elif o in ("-p", "--dbpassword"): dbpassword = a elif o in ("-n", "--newuser"): newuser = a elif o in ("-S", "--secret"): secret = a elif o in ("-N", "--fullname"): fullname = a elif o in ("-H", "--home"): home = a elif o in ("-q", "--quota"): quota = a elif o in ("-s", "--shared"): shared = a else: usage() sys.exit() # Use silly default values # These are the uid and the gid of the mail user resp. group on my # system -- you may want to override this! try: os.setegid(GID) os.seteuid(UID) except OSError: print ''' I can't change the uid or gid. If you continue, check permissions! Otherwise start as mail user or trustfully as root ;-) ''' if not database: database=DATABASE if not dbuser: dbuser=DBUSER if not dbpassword: dbpassword=DBPASSWORD if not quota: quota='' # No default values possible, so ask for values if not newuser: newuser=prompt("New user (eMail): ") if not secret: secret=prompt("New user (password): ") if not fullname: fullname=prompt("Full name: ") if not home: home=prompt("Home Directory: ") maildir="%s/Maildir"%home # Create directories try: os.makedirs(home,0700) except OSError: print "Cannot create directory %s"%home sys.exit(3) retval=os.system("/usr/bin/maildirmake %s"%maildir) if retval!= 0: print "Cannot create maildir %s"%maildir sys.exit(3) # Add user to the database connobj = connect(db=database, host='localhost', user=dbuser, passwd=dbpassword) cursobj = connobj.cursor() cursobj.execute("INSERT INTO passwd values (%s,encrypt(%s),%s, %s, %s, %s, %s, %s, %s)",(newuser, secret, secret, fullname, UID, GID, home, maildir,quota)) def prompt(prompt): return raw_input(prompt).strip() def usage(): print ''' maildb_adduser Script for adding users to a mail enviroment like the one described in 'Virtual Domains with Exim + Courier.IMAP + MySQL' (http://www.tty1.net//virtual_domains_en.html) See this document for further details. Parameters -h --help This text -d --database Database name (default: %s) -u --dbuser Database user (default: %s) -p --dbpassword Database password (default: %s) -n --newuser email of new user (default: Interactive input) -S --secret password of new user (default: Interactive input) -N --fullname full name of new user (default: Interactive input) -H --home home dierctory of new user (default: Interactive input) -q --quota quota of new user (default: EMPTY) -s --shared name of a shared dictionary (default: EMPTY) This script is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. '''%(DATABASE,DBUSER,DBPASSWORD) if __name__ == "__main__": main()