ltcp/aufgabe3.5/ldap-tools/ldap-users2ldif

66 lines
1.5 KiB
Plaintext
Raw Normal View History

2013-11-14 11:45:22 +00:00
#!/usr/bin/python
import sys
import crypt
import base64
import grp
import pwd
ldif_add = """dn: cn=%(name)s,ou=users,dc=zotac,dc=lctp
gidNumber: %(gid)s
cn: %(name)s
homeDirectory: /home/%(name)s
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: shadowAccount
uidNumber: %(uid)s
loginShell: /bin/bash
uid: %(name)s
userPassword: {CRYPT}%(password)s
sn: %(name)s"""
ldif_group = """dn: cn=%(name)s,ou=groups,dc=zotac,dc=lctp
objectClass: top
objectClass: posixGroup
gidNumber: %(gid)s
cn: %(name)s"""
if len(sys.argv) < 2:
print("Usage: %s <users-file>" % sys.argv[0], file=sys.stderr)
exit(1)
users = [pw.pw_name for pw in pwd.getpwall()]
groups = [gr.gr_name for gr in grp.getgrall()]
last_uid = max([user.pw_uid for user in pwd.getpwall()])
last_gid = max([user.gr_gid for user in grp.getgrall()])
for line in open(sys.argv[1]).readlines():
line = line.strip()
user, password = line.split(maxsplit=1)
if user in users:
print("Skipping user %s (multiple occurences or user already existing)" % user, file=sys.stderr)
continue
if user in groups:
print("Skipping group %s (multiple occurences or group already existing)" % user, file=sys.stderr)
continue
users.append(user)
groups.append(user)
print(ldif_add % {
"name": user,
"password": crypt.crypt(password),
"uid": last_uid + 1,
"gid": last_gid + 1
} + "\n")
print(ldif_group % {
"name": user,
"gid": last_gid + 1
} + "\n")
last_uid += 1
last_gid += 1