#!/usr/bin/env python import commands, os, sys out=sys.stdout err=sys.stderr import py import uconf mailfn = "/etc/mail/user" userlist = [ ] def inrange(uid): return 1000 <= uid < 2000 def getpassword(): status,output = commands.getstatusoutput('passook') p = output.strip().lower() r = open('/dev/random', 'r') salt = r.read(8) r.close() salt = "".join([ITOA64[ord(x) % 64] for x in salt]) return p, unix_md5_crypt(p, salt) class Passwd: def __init__(self): self.reread() def reread(self): lines = open('/etc/passwd').readlines() self.entries = map(lambda x: x.split(':'), lines) uids = map(lambda x: int(x[2]), self.entries) uids = filter(inrange, uids) self.uids = uids def user_exists(self, name): for entry in self.entries: if name == entry[0]: return True def next_uid(self): for i in range(1000,1500): if i not in self.uids: self.uids.append(i) break return i def execcmd(cmd): status, output = commands.getstatusoutput(cmd) if status: print "failed:",cmd print status,output raise SystemExit print >>err, " exec_ok:",cmd #def execcmd(cmd): # print "execing:", cmd passwd = Passwd() def make_user(username, realname, email): global mailfn if passwd.user_exists(username): usage("user exists: %s" %username) uid = passwd.next_uid() password = uconf.Password() crypt = password.hashspec clear = password.clear if not uconf.system.hasgroup(username): cmd = 'groupadd %(username)s' % locals() execcmd(cmd) cmd = "adduser -c '%(realname)s' -u %(uid)d -g %(username)s -G svnusers -p '%(crypt)s' -s /bin/bash %(username)s" % locals() execcmd(cmd) cmd = 'mkdir -p /home/%(username)s/.ssh' % locals() execcmd(cmd) cmd = 'touch /home/%(username)s/.ssh/authorized_keys' % locals() execcmd(cmd) cmd = 'echo "%(clear)s" >/home/%(username)s/.ssh/password' % locals() execcmd(cmd) cmd = 'chown -R %(username)s:%(username)s /home/%(username)s' % locals() execcmd(cmd) cmd = 'chmod -R 755 /home/%(username)s' % locals() execcmd(cmd) cmd = 'chmod 700 /home/%(username)s/.ssh' % locals() execcmd(cmd) cmd = 'chmod 700 /home/%(username)s/.ssh/password' % locals() execcmd(cmd) print "codespeak account created for %(realname)s (%(email)s)" % locals() print "%(username)s pass: %(clear)s" % locals() mailfn = py.path.local(mailfn) lines = mailfn.readlines(cr=0) lines.append("%s: %s" %(username, email)) mailfn.write("\n".join(lines)) print "username/email info written to", mailfn def usage(msg): print 'usage: %s "username" "realname" "email"' % sys.argv[0] print msg raise SystemExit, 1 if __name__ == '__main__': args = sys.argv[1:] if len(args) != 3: usage("need exactly three arguments") username, realname, email = args assert "@" in email, "%r must be a valid email-adrress" % email make_user(username, realname, email)